开发者

How do I redirect all but one url to a script

I'm trying to get www.example.com and www.example.com/index.html to go to index.html, but I want all other urls e.g. www.example.com/this/is/another/link to still show www.example.com/this/is/another/link but be processed by a generic script. I've tried

RewriteEngine on
RewriteCond %{REQUEST_URI} !^index\.html$
RewriteCond %{REQUEST_URI} !^$
R开发者_Go百科ewriteRule ^(.*)$ mygenericscript.php [L]

but it wont work, can someone please help?


Instead of testing what %{REQUEST_URI} is, you can instead just test if the resource exists:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* mygenericscript.php

This prevents your static resources (images, stylesheets, etc.) from being redirected if they're handled through the same directory your .htaccess is in as well.

What's probably happening now is that you're seeing an internal server error, caused by an infinite internal redirection loop when you try to access anything that isn't / or /index.html. This is because .* matches every request, and after you rewrite to mygenericscript.php the first time, the rule set is reprocessed (because of how mod_rewrite works in the context that you're using it in).


The easiest to do this is to install a 404-handler which gets executed when the server does not find a file to display.

ErrorDocument 404 /mygenericscript.php

or

ErrorDocument 404 /cgi-bin/handler.cgi

or similar should do the trick.

It is not that RewriteRule's can not be used for this, it is just that they are tricky to set up and requires in depth knowledge on how apache handles requests. It is a bit of a black art.


It appears as if you're using PHP, and you can use auto_x_file (x is either append or prepend:

http://php.net/manual/en/ini.core.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜