如何使用.htaccess

时间:2015-10-08 17:43:55

标签: apache .htaccess mod-rewrite

如何从我的URL中的目录列表中获取每个现有PHP文件的$ _GET请求,如下所示:

# Scenario 1: Assuming existence of file.php
    URL like: /path/to/file/v1/v2/v3 ...
    Act like: /path/to/file.php?v1&v2&v3
              or
              /path/to/file.php?query=[v1,v2,v3 ...]
    HTTP GET: array(v1 => null,v2 => null, v3 => null, ...) or array(v1, v2, v3)
    or
    HTTP GET: array(query => [v1,v2,v3])

# Scenario 2: Assuming existence of file.php
    URL like: /path/to/file/k1/v1/k2/v2 ...
    Act like: /path/to/file.php?k1=v1&k2=v2 ...
    HTTP GET: array(k1 => v1, k2 => v2, ...)

由于我对Apache配置不够熟悉,希望有人可以帮助我。

请注意,我的.htaccess文件中出于其他原因我已经拥有这些代码:

# To remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L]

# To remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

# To check whether the PHP file exists and set it back internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^.*$ $0.php [L]

# To redirect /index to root
RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

由于

1 个答案:

答案 0 :(得分:1)

您必须使用mod-rewrite根据查询字符串重定向:

请确保在httpd.conf中取消注释以下行:

LoadModule rewrite_module modules/mod_rewrite.so

这是一个例子:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)thiscontent=2660(&|$)
RewriteRule ^/?applications/newWeb/www/index\.php$ http://www.example.come/index.php/folder1/foler2/folder-resources? [L,R=301]