.htaccess重写多个网址

时间:2013-11-09 13:25:08

标签: php .htaccess url-rewriting rewrite

我需要帮助重写.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。但是个人资料页面确实给了我/ 用户名

2 个答案:

答案 0 :(得分:1)

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]

答案 1 :(得分:0)

您的重写规则基于正则表达式,因此需要尽可能具体,以便服务器可以准确确定要使用的网址 - 例如,如何判断http://example.com/something是页面还是个人资料?在您的网址上使用“用户”,“个人资料”等前缀意味着可以将http://example.com/profile/something重定向为具有默认重定向的用户名。要实现此目的,您需要首先使用更具体的模式匹配(用户),并使用[L]指令指示不应处理以下规则。我通常使用负字符类来匹配任何正斜杠之外的任何内容 - [^/]*

# 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