2015-10-12 77 views
1

这里是我的.htaccess文件:如何重写URL路径查询字符串

# 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] 

假设有根目录和V1,V2一个file.php,......是我的查询字符串输入和该URL请求如下:

http://domain.tld/file/v1/v2... 

我只需要使用的.htaccess送V1,V2,...如HTTP GET到file.php,它应该与任何其他PHP文件名和在工作的情况下,可能有任何数量的输入,否则处理多达3个输入就足够了。

回答

1

我假设file.php只是一个模板名称,并不局限于那个特定的名称。您可以尝试:现在

RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^([^/]+)/(.+)$ $1.php?params=$2 [NC,L] 

,在file.php,你会得到通过查询参数为:

$_GET['params'] // which will be of the form v1/v2/v3... 
+0

谢谢,我应该只是没有任何其他的变化在第一我的.htaccess中添加此到以前的代码? – 4L3X

+0

@ 4L3X在删除'.php'后立即放置。 – hjpotter92

+0

不幸的是没有工作,并导致内部服务器错误。 – 4L3X