Php .htaccess重写多个URL
我需要在.htaccess文件中进行重写的帮助。 这就是我现在所说的,但当我尝试添加一个新的重写规则时,什么都没有发生。 我想重写的url是index.php?page=$1Php .htaccess重写多个URL,php,.htaccess,url-rewriting,rewrite,Php,.htaccess,Url Rewriting,Rewrite,我需要在.htaccess文件中进行重写的帮助。 这就是我现在所说的,但当我尝试添加一个新的重写规则时,什么都没有发生。 我想重写的url是index.php?page=$1 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ profile.php?username=$1 所以当我这样做的时候: RewriteEngine On
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。但是个人资料页面确实提供了/username
如果您要求服务器将每个URL重定向到两个不同的页面,则无法工作服务器无法猜测要加载的页面
您需要的是/profile/username规则或/page/pagetitle规则。
例如:
RewriteRule ^profile/(.*)$ profile.php?username=$1 [QSA]
RewriteRule ^(.*)$ index.php?page=$1 [L]
您的重写规则基于正则表达式,因此需要尽可能具体,以便服务器可以准确确定要使用的url—例如,您如何判断是页面还是配置文件?在URL上使用前缀,如“user”、“profile”等意味着可以将其重定向为用户名,并对其他所有内容进行默认重定向。要实现这一点,您需要首先使更具体的模式匹配(用户),并使用
[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]
在进行测试(请注意,%{REQUEST\u FILENAME}
和%{REQUEST\u 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
页面
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
你必须给这个问题增加更多的细节。你问什么还不清楚。“什么都没发生”也帮不了什么忙。您想添加什么规则,如何添加?你想发生什么?请使用上面的
编辑
按钮并添加更多详细信息。没有其他方法可以实现同样的效果吗?我不介意重做.htaccess。您的示例与原始问题中的模式完全匹配,因为^(.*)$
也将匹配^profile/(.*)$
。检查测试。感谢链接。但我不知道哪里出了问题,但当我使用你的规则时,它也不会起作用。