【问题标题】:.htaccess rewrite rule causes wrong links.htaccess 重写规则导致错误的链接
【发布时间】:2015-03-15 15:08:44
【问题描述】:

我尝试为我的网站制作漂亮的网址,例如: http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049

我的 .htaccess 文件如下所示:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f


RewriteRule ^profile/(\d+)*$ ./profile.php?id=$1

它可以工作,root/profile/123 打开 root/profile.php?id=123,但文件夹路径现在已损坏:

root/profile.php?id=123 引用例如 root/css/style.css

但是 root/profile/123 想要引用 root/profile/css/style.css,它不存在。

我想让它同时工作:root/profile.php?id=123 和 root/profile/123

如何解决链接问题?

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    您可以为通过/root/profile/Alias 访问的任何内容添加别名:

    Alias /root/profile /real/path/root 
    

    http://httpd.apache.org/docs/2.4/mod/mod_alias.html#alias

    如果您仅限于 .htaccess 文件,您可以在已有的规则之前添加另一个重写规则:

    RewriteRule ^root/profile/(.*)$ /profile/$1  [L]
    RewriteRule ^profile/(\d+)*$ /profile.php?id=$1
    

    由于第一条规则将对根/配置文件中的文件的任何访问重定向到配置文件,这反过来由第二个重写器评估,这将导致无限循环。代表“last”的标志[L] 告诉重写引擎在遇到以“root/profile”开头的路径时停止进一步处理。

    【讨论】:

    • 抱歉,我目前无法测试。错误是什么? - 在正则表达式中的.css 之前添加了一个反斜杠。
    • profile.php 和 .htaccess 在同一目录中 - 错误 500 - AliasMatch ^profile(.*) ./$1
    • 我想正确引用所有文件(链接、图片、css、javascript等)
    • 这种情况下应该更简单,用alias;查看更新。
    • 我试过 Alias ./profile ./ 但错误 500 仍然存在。也许我必须在 apache 的任何地方激活别名?或者它不适用于相对路径?
    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多