0

我需要在 .htaccess 文件中进行重写的帮助。所以这就是我现在所拥有的,但是当我尝试添加一个新的 RewriteRule 时,什么也没有发生。我要重写的网址是 index.php?page=$1

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1

所以当我这样做时:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

当我这样做时,该页面没有任何 CSS:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*_)$ index.php?page=$1

该页面有 css,但我仍然得到 index.php?page=pagetitle。但是个人资料页面确实给了我/用户名

4

2 回答 2

0

您的重写规则基于正则表达式,因此需要尽可能具体,以便服务器可以准确确定要使用的 url - 例如,您如何判断http://example.com/something是页面还是配置文件? 在您的 URL 上使用诸如“user”、“profile”等前缀意味着http://example.com/profile/something可以作为用户名重定向,而其他所有内容的默认重定向。要做到这一点,您需要首先进行更具体的模式匹配(用户)并使用该[L]指令指示不应处理以下规则。我通常对 URL 使用负字符类来匹配正斜杠之外的任何内容 - [^/]*

# Enable mod_rewrite
RewriteEngine On
# Set the base directory
RewriteBase /
# Don't process if this is an actual file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Does this url start with /profile and then followed with additional characters?
RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]
# Assume everything else is a page
RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

http://htaccess.madewithlove.be/进行测试(请注意,%{REQUEST_FILENAME}%{REQUEST_FILENAME}支持测试)。

轮廓

input url
http://www.example.com/profile/something

output url
http://www.example.com/profile.php

debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,QSA,L]  
    This rule was met, the new url is http://www.example.com/profile.php
    The tests are stopped because the L in your RewriteRule options
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]

input url
http://www.example.com/something

output url
http://www.example.com/index.php

debugging info
1 RewriteRule ^profile/([^/]*)$ profile.php?username=$1 [NC,L]  
2 RewriteRule ^(.*)$ index.php?page=$1 [NC,L]
    This rule was met, the new url is http://www.example.com/index.php
    The tests are stopped because the L in your RewriteRule options
于 2013-11-09T16:07:26.280 回答
0
RewriteRule ^(.*)$ profile.php?username=$1
RewriteRule ^(.*)$ index.php?page=$1

您要求服务器将每个 URL 重定向到两个不同的页面,它无法工作服务器不能只是猜测要加载哪个页面。

您需要的是 /profile/username 规则或 /page/pagetitle 规则。IE 类似:

RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]
于 2013-11-09T13:46:43.483 回答