【问题标题】:Vanity/Clean URL with Two Parameters带有两个参数的虚荣/干净的 URL
【发布时间】:2014-09-11 07:56:24
【问题描述】:

所以我有一个 .htaccess 文件将 /username 放入 profile.php?id=$0

我需要能够接受两个参数,而不会与从 php 文件中删除 .php 扩展名的重写规则发生冲突。

这是我想要的一个明确示例:

/profile.php?id=username&category=schooling ------> /username/schooling

我不希望这影响其他 php 文件,例如 /about

我的.htaccess文件如下:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ $1.php
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$0 [NC]

任何帮助将不胜感激,谢谢! :)


更新:

所以这对我有用,但图像不起作用

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule ^([A-Za-z0-9]+)$ $1.php [L]
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$0 [L]
RewriteRule ^([^/]*)/([^/]*)$ /profile.php?id=$1&category=$2 [L]

有什么想法吗?我知道它背后的原因,但不知道如何解决它。我认为图像网址也被重定向了。

【问题讨论】:

  • 抱歉,我编辑了我的原始问题并提供了对我有用的更新。在该更新中,一切正常,即使没有任何意义,但网站上的图像没有显示。我认为它们被视为 profile.php 重定向。 @zx81

标签: php .htaccess url mod-rewrite url-rewriting


【解决方案1】:

也许

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/([A-Za-z0-9\._\-]+)/([A-Za-z0-9]+)$ profile.php?id=$1&category=$2 [L, QSA, NC]
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$1 [L, QSA, NC]
RewriteRule ^([A-Za-z0-9]+)$ $1.php [L, QSA]

如果L 的规则匹配,L 标志用于停止 mod_rewrite 表单处理剩余的规则。

【讨论】:

  • 这给了我每个页面上的内部服务器错误,包括未重写的页面。
【解决方案2】:

试试这个:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# IF we have an existing file or folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
# THEN leave the url unchanged, and skip the next two rules
RewriteRule ^ - [L,S=2]
# ELSE (i.e., it's a pretty url that doesn't correspond to a real file or folder)
# Apply these rules if they match
RewriteRule ^(\w+)/?$ profile.php?id=$1 [L,QSA]
RewriteRule ^(\w+)/(\w+)/?$ profile.php?id=$1&category=$2 [L,QSA]

【讨论】:

  • 我在每个页面上都收到 404 错误,包括基本的 php 文件,例如 /about
  • 这跟我在子域上工作有什么关系吗?只是想知道,我认为它不会有影响。
  • 好吧,.htaccess 必须在正确的文件夹中。假设文件位于somefolder。 url /somefolder/a/b 将重定向到somefolder/profile.php?etc 该文件是否存在?如果没有,我们将得到 404。所以我们要么需要修复路径,要么将 htaccess 放在正确的位置。
  • 我在子域的根目录中有 .htaccess 文件。我已经用基本的重写对其进行了测试,所以我知道它有效。很奇怪
  • 可以暂时把规则里的L改成R吗?这样,您将能够在浏览器栏中看到我们被重定向到的位置,以及为什么该文件不存在。谢谢。
猜你喜欢
  • 2018-11-29
  • 2013-03-22
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 2014-11-10
  • 1970-01-01
相关资源
最近更新 更多