【问题标题】:URL Rewrite /index.php?page=sample to /sample.htmURL 重写 /index.php?page=sample 到 /sample.htm
【发布时间】:2013-12-09 23:29:52
【问题描述】:

对.htaccess 非常陌生,大部分内容并不完全理解。无论如何,目的是将/index.php?page=sample重写为sample.htm。 (或任何其他页面)

下面的脚本似乎可以部分工作。如果我键入 /sitemap.htm,它会显示该页面,但地址栏会更改为 /index.php?page=sitemap。如何将网址重写为更友好的网址,以便当我输入 /sitemap.htm 时地址保持不变,当我输入 /index.php?page=sitemap 时,它会重定向到 /sitemap.htm 但仍然有效?

抱歉,如果我听起来令人困惑。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+getreal\.co\.uk/index\.php\?page=([^&\s]+) [NC]
RewriteRule ^ %1.htm? [L,R=301]

RewriteRule ^(.+?)\.htm?$ index.php?page=$1 [L,R=301]

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    你已经很接近了。但是THE_REQUEST 不包含域名。

    试试这个代码:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /test/
    
    RewriteCond %{THE_REQUEST} /index\.php\?page=((?!login\b)[^&\s]+) [NC]
    RewriteRule ^ %1.htm? [L,R=302]
    
    RewriteRule ^(.+?)\.htm$ index.php?page=$1 [L,NC,QSA]
    

    【讨论】:

    • 嗨 Anubhava,这确实改变了 url,但它进入了一个循环错误:'This web page has a redirect loop'
    • 现在已修复。没有意识到您在第二条规则中也有R=301
    • 您好 Anubhava,非常感谢。这行得通。我还有另一个问题。应用此功能后,我无法再登录/注册该站点。有没有办法忽略 /index.php?page=login ?
    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多