开发者

htaccess rewriterule

A fairly basic htaccess question, but I don't use it much, so i have no idea. I want to check if the requested file exists. If it does, forward to one page, and if not forwar开发者_开发技巧d to another page and pass the requested path as GET.

Thanks in advance.


RewriteEngine On

# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

# if not, pass it to index.php
RewriteRule ^(.*) index.php?page=$1 [QSA]

As Gumbo suggested, you can repeat the condition without the ! if you also want to rewrite the URL when a file exists. Maybe you want to 'protect' your real files and folders with this method.


Use the -f expression in a RewriteCond condition to test if the given path points to an existing regular file:

RewriteEngin on

# file exists    
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ one-page [L]

# file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ another-page [L]

The initial requested URI should then be available in an environment variable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜