【问题标题】:.htaccess rewrite multiple urls.htaccess 重写多个 url
【发布时间】:2013-11-09 13:25:07
【问题描述】:

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

【问题讨论】:

  • 您必须在问题中添加更多详细信息。目前还不清楚你问什么。 “什么都没有发生”没有任何帮助。你想添加什么规则,你是怎么做的?你想发生什么?请使用上方的edit 按钮并添加更多详细信息。

标签: php .htaccess url-rewriting rewrite


【解决方案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]

【讨论】:

  • 没有其他方法可以达到同样的效果吗?我不介意重做 .htaccess
  • 您的示例匹配与原始问题完全相同的模式,因为^(.*)$ 也将匹配^profile/(.*)$。查看htaccess.madewithlove.be 进行测试。
【解决方案2】:

您的重写规则基于正则表达式,因此需要尽可能具体,以便服务器可以准确确定要使用的 url - 例如,您如何判断 http://example.com/something 是页面还是配置文件?在您的 URL 上使用诸如“用户”、“个人资料”等前缀意味着 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

【讨论】:

  • 感谢您的链接。但我不知道哪里出了问题,但是当我使用你的规则时它也不起作用。
猜你喜欢
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2019-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多