Mod重写和静态页面 [英] mod rewrite and static pages
问题描述
是否可以排除通过mod rewrite解析的URL? 我的.htaccess具有重写规则,例如
is possible to exclude a url being parsed by mod rewrite? my .htaccess has rewrite rules like
RewriteRule ^contact contact_us.php
和另外两个静态页面.
and a couple more static pages.
当前我的网站没有问题,原因是使用 http://domain.com/user .php?user =用户名 但现在我需要重写为:
currently my site don't have troubles cause uses http://domain.com/user.php?user=username but now i need rewrite to:
http://domain.com/username
我尝试过:
RewriteRule ^(.*)$ user.php?user=$1 [L]
但是我所有的站点都停止工作...
but all my site stops working...
是否可以避免将我的静态页面(如联系人/提要/等)解析为用户名?
is possible to avoid parse my static pages like contact/feed/etc being treated like usernames?
修改以匹配大卫的要求:
edit to match david req:
这是我实际的.htaccess文件:
this is my actual .htaccess file:
RewriteEngine On
Options +Followsymlinks
RewriteRule ^contact contact_us.php [L]
RewriteRule ^terms terms_of_use.php [L]
RewriteRule ^register register.php [L]
RewriteRule ^login login.php [L]
RewriteRule ^logout logout.php [L]
RewriteRule ^posts/(.*)/(.*) viewupdates.php?username=$1&page=$2
RewriteRule ^post(.*)/([0-9]*)$ viewupdate.php?title=$1&id=$2
RewriteRule ^(.*)$ profile.php?username=$1 [L]
我还启用了我的第一个文件的modrewrite日志: http://pastie.org/1044881
also i've enabled modrewrite log my first file:http://pastie.org/1044881
推荐答案
首先放置静态页面的重写规则,并在其中添加[L]
标志:
Put the rewrite rules for the static pages first, and add the [L]
flag to them:
RewriteRule ^contact contact_us.php [L]
...
然后在之后,使用您的用户名重写规则:
then after those, use your rewrite rule for the username:
RewriteRule ^(.*)$ user.php?user=$1 [L]
(希望没有人使用用户名contact
).
(hopefully nobody has a username of contact
).
编辑:基于您发布的日志输出(我认为这对应于未成功尝试访问contact
页面的信息……对吗?) ,请尝试将contact
重写规则更改为
EDIT: Based on the log output you posted (which I'm assuming corresponds to an unsuccessful attempt to access the contact
page... right?), try changing the contact
rewrite rule to either
RewriteRule ^contact$ contact_us.php [L]
或
RewriteRule ^contact contact_us.php [L,NS]
也就是说,添加$
以使模式仅与文字URL contact
匹配,或者添加NS
标志以防止其应用于子请求.根据日志输出,似乎发生的事情是Apache将contact
重写为contact_us.php
,然后对该新URL执行内部子请求.到目前为止,一切都很好.奇怪的是^contact
模式再次与contact_us.php
匹配,将其转换"为contact_us.php
,即同一件事,Apache将其解释为信号,它应该完全忽略该规则.现在,我认为 Apache可以只在子请求上忽略该规则,但是我不确定它是否在忽略整个重写过程并保留原来的规则网址/contact
照原样.如果是这种情况,请执行我建议的更改之一.
That is, either add $
to make the pattern match only the literal URL contact
, or add the NS
flag to keep it from applying to subrequests. According to the log output, what seems to have happened is that Apache rewrites contact
to contact_us.php
and then does an internal subrequest for that new URL. So far so good. The weird thing is that the ^contact
pattern again matches contact_us.php
, "transforming" it to contact_us.php
, i.e. the same thing, which Apache interprets as a signal that it should ignore the rule entirely. Now, I would think Apache would have the sense to ignore the rule only on the subrequest, but I'm not sure if it's ignoring the entire rewriting process and leaving the original URL, /contact
, as is. If that's the case, making one of the changes I suggested should fix it.
编辑2 :您的重写日志摘录使我想起了一些事情:我建议制定重写规则
EDIT 2: your rewrite log excerpt reminded me of something: I'd suggest making the rewrite rule
RewriteRule ^([^/]+)$ user.php?user=$1 [L]
因为斜杠不应在任何用户名中出现. (对吗?)或者你可以做
since slashes shouldn't be occurring in any usernames. (Right?) Or you could do
RewriteRule ^(\w+)$ user.php?user=$1 [L]
如果用户名只能包含单词字符(字母,数字和下划线).基本上,使正则表达式仅匹配可能是有效用户名的任何字符序列,但不匹配图像或CSS/JS文件的URL.
if usernames can only include word characters (letters, numbers, and underscore). Basically, make a regular expression that matches only any sequence of characters that could be a valid username, but doesn't match URLs of images or CSS/JS files.
这篇关于Mod重写和静态页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!