mod重写和静态页面
是否可以排除 mod 重写正在解析的 url? 我的 .htaccess 有重写规则
RewriteRule ^contact contact_us.php
和更多静态页面。
目前我的网站没有问题,因为使用 http://domain.com/user.php?用户=用户名 但现在我需要重写为:
http://domain.com/username
我已经尝试过:
RewriteRule ^(.*)$ user.php?user=$1 [L]
但我的所有网站都停止工作...
是否可以避免解析我的静态页面,如联系人/提要/等,并被视为用户名?
编辑以匹配 david 要求:
这是我实际的 .htaccess 文件:
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
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.
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
I've tried with:
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:
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]
also i've enabled modrewrite log my first file:http://pastie.org/1044881
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先放置静态页面的重写规则,并向其添加
[L]
标志:然后在之后,使用您的用户名重写规则:(
希望没有人知道用户名
contact
)。编辑:根据您发布的日志输出(我假设对应于访问
联系
页面的不成功尝试...对?),尝试将contact
重写规则更改为或
即,添加
$
以使模式仅匹配文字 URLcontact
,或者添加NS
标志以防止其应用于子请求。根据日志输出,似乎发生的情况是 Apache 将contact
重写为contact_us.php
,然后对该新 URL 执行内部子请求。到目前为止,一切都很好。奇怪的是^contact
模式再次匹配contact_us.php
,将其“转换”为contact_us.php
,即相同的事情, Apache 将其解释为一个信号,表明它应该完全忽略该规则。现在,我认为 Apache 会只忽略子请求上的规则,但我不确定它是否忽略整个重写过程并保留原始的URL,/contact
,按原样。如果是这种情况,进行我建议的更改之一应该可以解决它。编辑2:您的重写日志摘录让我想起了一件事:我建议制定重写规则,
因为斜杠不应出现在任何用户名中。 (对吗?)或者
如果用户名只能包含单词字符(字母、数字和下划线), 您也可以这样做。基本上,创建一个正则表达式,仅匹配可能是有效用户名的任何字符序列,但不匹配图像或 CSS/JS 文件的 URL。
Put the rewrite rules for the static pages first, and add the
[L]
flag to them:then after those, use your rewrite rule for the username:
(hopefully nobody has a username of
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 thecontact
rewrite rule to eitheror
That is, either add
$
to make the pattern match only the literal URLcontact
, or add theNS
flag to keep it from applying to subrequests. According to the log output, what seems to have happened is that Apache rewritescontact
tocontact_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 matchescontact_us.php
, "transforming" it tocontact_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.EDIT 2: your rewrite log excerpt reminded me of something: I'd suggest making the rewrite rule
since slashes shouldn't be occurring in any usernames. (Right?) Or you could do
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.
RewriteCond
的-f
和-d
选项检查当前匹配是否是磁盘上的文件或目录。The
-f
and-d
options toRewriteCond
check if the current match is a file or directory on disk.