2017-04-12 92 views
0

对于域www.example.com,我希望将用户引导至index.php。使用只在特定情况下试图通过htaccess重写网址

DirectoryIndex index.php 

我这样做。但是,如果他们有一个斜线和非文件类型的值,我想将它们重定向到一个不同的页面,并使用该值作为一个PHP变量得到:

www.example.com/hello lands on a_different_page.php?var=hello 

但我不想打扰以“php”结尾的请求例如:

www.example.com/page.php 

不应尝试重定向。同时还为普通的URL的请求不应得到改变:

www.example.com goes to index.php 

我已经找到办法,使其中的一些发生,但他们总是与其他请求非预期的后果干扰。任何帮助将不胜感激 - 我对htaccess重写的研究没有帮助。

回答

0

您可以使用下面的htaccess:

DirectoryIndex index.php 
RewriteEngine on 
#rewrite /foobar unless a real file and dir to /page.php?var=foobar 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.+) /page.php?var=$1 [L] 
+0

诀窍!谢谢。 – Jim

0

处理这个问题的最好方法是不重定向任何指向已存在的东西。

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule (.*) a_different_page.php?var=$1 
0

希望它会正常工作。对我来说工作完全没问题。

RewriteEngine On 

Options -Multiviews 
#For rewriting/request to index.php 
RewriteCond %{REQUEST_URI} ^/?$ 
RewriteRule ^(.*)$ /index.php [L] 

#For redirecting /some/anonymous/request to some_different_page.php?var=/some/anonymous/request when request uri does not ends with .php 
RewriteCond %{REQUEST_URI} !\.(php)$ 
RewriteRule ^(.*)$ /some_different_page.php?var=$1 [R=301,L,QSA]