开发者

Rewrite rules confused

开发者 https://www.devze.com 2023-01-22 10:56 出处:网络
I am struggling to achieve this simple thing... I have some static pages which should be like www.domain.com/profile etc..

I am struggling to achieve this simple thing...

I have some static pages which should be like

www.domain.com/profile etc.. The problem is how to write the r开发者_运维知识库ewrite rules in order to ..

There would be some fixed rewrites like /home

I want every file that exists not to be rewritten www.domain.com/test.php should go to test.php

Lastly if it is not found i want it to be redirected to static.php?_.....

RewriteRule ^/home/?$ /index.php?__i18n_language=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ /static.php?__i18n_language=$1  

This works ok but if i type index.php or test.php or even the mach from other redirection it gets me in static.php... Please help!


According to your description you can use these rules:

# stop rewriting process if request can be mapped onto existing file
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteRule ^ - [L]

# rewrite known paths /home, /foo, /bar, etc.
RewriteRule ^/(home|foo|bar|…)$ /index.php [L]

# rewrite any other path
RewriteRule ^ /static.php [L]


I haven't used this in a long time, but it's something I found, that should help. It is part of an old script that generates .httaccess files for redirecting from /usr/share/doc only when the doc isn't found: The rule is "Check, and if the target url exists, then leave":

RewriteEngine On
RewriteBase /doc/User_Documents

### If the directory already exists, then leave
### We're just redirecting request when the directory exists but has been renamed.

RewriteCond %{REQUEST_FILENAME} User_Documents/([^/]+-[0-9][^/]*)
RewriteCond $PWD/%1 -d
RewriteRule .* - [L]

It's the [L] that means leave if one of the conditions is matched. In the old script, there are hundreds of generated rules (after [L]) that are skipped, because one of the conditions matched. In your case you would skip the rest of the rules when the target %{REQUEST_FILENAME} is found.

So, I suggest, before the redirection rule:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
0

精彩评论

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

关注公众号