【问题标题】:flat url rewriting: stripped of path in HTML anchor平面 url 重写:去除 HTML 锚中的路径
【发布时间】:2017-02-23 13:34:01
【问题描述】:

在 C:\xampp\htdocs\myname\myproject\index.html 我有

<a href="/aboutus">About Us</a>

这应该链接到 C:\xampp\htdocs\myname\myproject\aboutus.html。

在我的本地主机中,我希望 url 看起来像这样:

localhost/myname/myproject/
localhost/myname/myproject/aboutus

但是,我似乎无法弄清楚如何编写 url 重写规则来完成这项工作。 唯一有效的是,如果我将路径放在 html 中:

<a href="/myname/myproject/aboutus">About Us</a>

但我不想那样做。

我的 C:\xampp\htdocs\myname\myproject\.htaccess 文件:

<IfModule mod_rewrite.c>
# Enable url rewriting
RewriteEngine On 

RewriteBase /myname/myproject/

# Don't rewrite explictly requested paths
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d   

# URL rewriting rules
#TEST RewriteRule aboutus /myname/myproject/aboutus.html
RewriteRule ^([0-9]+).html/$ $1\.html

</IfModule>

我在这里做错了什么?

【问题讨论】:

    标签: html .htaccess url-rewriting


    【解决方案1】:

    尝试改变:

    RewriteRule ^([0-9]+).html/$ $1\.html

    RewriteRule ^([0-9A-Za-z\-_]+)/?$ $1\.html

    你做错的是正则表达式。您只允许页面名称中包含 0-9 字符。还要注意末尾的/?。这是为了让 / 成为可选的。

    【讨论】:

    • 谢谢!我感觉我的正则表达式会出错。但是问题似乎更深了,因为即使RewriteRule aboutus aboutus\.html 也不起作用(除非我将路径放在 href 标记中)。它总是重定向到localhost/xampp
    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多