【问题标题】:Htaccess rewrite rule compeletly works in localhost but only partially working on serverHtaccess 重写规则完全适用于本地主机,但仅部分适用于服务器
【发布时间】:2016-05-22 15:04:13
【问题描述】:

我遇到了一个问题,我的 htaccess 重写命令在本地主机上运行,​​但在上传到服务器时它们部分运行。

Options +FollowSymLinks
Options -Multiviews
#Turn Rewrite Engine on
RewriteEngine on
#rewrite for pages
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteRule ^about about.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^privacy privacy.php [NC,L]
RewriteRule ^terms terms.php [NC,L]
RewriteRule ^contact contact.php [NC,L]
RewriteRule ^submit submit.php [NC,L]
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?article_id=$1&title=$2 [NC,L]

以下所有单数重写规则在本地主机和服务器上都可以正常工作:

RewriteRule ^about about.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^privacy privacy.php [NC,L]
RewriteRule ^terms terms.php [NC,L]
RewriteRule ^contact contact.php [NC,L]
RewriteRule ^submit submit.php [NC,L]

当我尝试使用 url 查询字符串重写规则时,它似乎无法在服务器上运行,但在 localhost 中运行良好:

RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?article_id=$1&title=$2 [NC,L]

我试图通过仅查询 id 来使其工作,但无济于事:

RewriteRule ^article/([0-9]+) article.php?article_id=$1 [NC,L]

我正在寻找的最终结果是 www.website.com/article/123/article-title

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    问题在于您的前 2 个条件被应用于不同的规则,

    改变

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    

    【讨论】:

    • 我已经按照你的要求做了,但并没有解决问题
    【解决方案2】:

    请您一次尝试以下规则之一

    规则 1

    RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ http://%{HTTP_HOST}/article.php?article_id=$1&title=$2 [NC,L]
    

    规则 2

    RewriteRule ^/article/([0-9]+)/([0-9a-zA-Z_-]+)$ http://%{HTTP_HOST}/article.php?article_id=$1&title=$2 [NC,L]
    

    请求链接www.website.com/article/123/article-title。 如果这些都不起作用。然后将两个规则的标志从 [NC,L] 更改为 [L,R=301](仅用于测试目的),并告诉我在两种情况下请求 url 后浏览器地址栏显示的内容(在这里我已从内部重定向更改为外部重定向。所以重定向的地址应该在浏览器地址栏中)。

    更新:

    Options +FollowSymLinks
    Options -Multiviews
    #Turn Rewrite Engine on
    RewriteEngine on
    #rewrite for pages
    RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ http://%{HTTP_HOST}/article.php?article_id=$1&title=$2 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ^about about.php [NC,L]
    RewriteRule ^archive archive.php [NC,L]
    RewriteRule ^privacy privacy.php [NC,L]
    RewriteRule ^terms terms.php [NC,L]
    RewriteRule ^contact contact.php [NC,L]
    RewriteRule ^submit submit.php [NC,L]
    

    【讨论】:

    • 我已经按照你的要求做了,但并没有解决问题
    • 第一条规则在 localhost 和服务器上不起作用,当我将规则一从 NC 切换到 r=301 时,我得到了相同的结果。对于第二条规则,我得到与上面完全相同的结果,对于这两条规则,我确实在地址栏中得到 www.website.com/article/123/article-title 但没有页面
    • [NC,L] 更改为[L,R=301] 之后。当您从浏览器请求www.website.com/article/123/article-title 时,浏览器应该在地址中显示www.website.com/article.php?article_id=123&title=article-title。但是你是说请求的链接在浏览器的地址栏中没有变化吗?
    猜你喜欢
    • 2018-05-06
    • 2013-09-02
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2012-12-13
    • 2016-02-12
    相关资源
    最近更新 更多