mod重写和静态页面

发布于 2024-09-10 13:30:17 字数 1101 浏览 1 评论 0原文

是否可以排除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女七分熟 2024-09-17 13:30:17

首先放置静态页面的重写规则,并向其添加 [L] 标志:

RewriteRule ^contact contact_us.php [L]
...

然后之后,使用您的用户名重写规则:(

RewriteRule ^(.*)$ user.php?user=$1 [L]

希望没有人知道用户名contact)。

编辑:根据您发布的日志输出(我假设对应于访问联系页面的不成功尝试...对?),尝试将 contact 重写规则更改为

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 会只忽略子请求上的规则,但我不确定它是否忽略整个重写过程并保留原始的URL,/contact,按原样。如果是这种情况,进行我建议的更改之一应该可以解决它。

编辑2:您的重写日志摘录让我想起了一件事:我建议制定重写规则,

RewriteRule ^([^/]+)$ user.php?user=$1 [L]

因为斜杠不应出现在任何用户名中。 (对吗?)或者

RewriteRule ^(\w+)$ user.php?user=$1 [L]

如果用户名只能包含单词字符(字母、数字和下划线), 您也可以这样做。基本上,创建一个正则表达式,仅匹配可能是有效用户名的任何字符序列,但不匹配图像或 CSS/JS 文件的 URL。

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]

(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 the contact rewrite rule to either

RewriteRule ^contact$ contact_us.php [L]

or

RewriteRule ^contact contact_us.php [L,NS]

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.

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]

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.

慕烟庭风 2024-09-17 13:30:17

RewriteCond-f-d 选项检查当前匹配是否是磁盘上的文件或目录。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ....

The -f and -d options to RewriteCond check if the current match is a file or directory on disk.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文