【问题标题】:URL rewriting issue with the same starting name of url in .htaccess.htaccess 中 url 的起始名称相同的 URL 重写问题
【发布时间】:2017-02-04 16:09:00
【问题描述】:

我在访问具有相同起始 URL 名称的 URL 时遇到问题,如下所示:

RewriteEngine On    # Turn on the rewriting engine

RewriteRule    ^sections?$   sections.php   [NC,L]

RewriteRule    ^sections/?$   sections.php    [NC,L]

RewriteRule    ^sections/([0-9]+)/([a-zA-Z-.]+)/view?$   view_sections.php?id=$1&name=$2   [NC,L]

RewriteRule    ^sections/([0-9]+)/([a-zA-Z-.]+)/view/?$   view_sections.php?id=$1&name=$2    [NC,L]

前 2 条规则有效,但后 2 条无效,它只是加载sections.php 而不是查看sections.php。当我使用 Wamp 时它可以工作,但是当我在线上传时它不起作用。是不是因为php和apache的版本?

我的 Wamp 版本:

Apache 版本:2.4.9

PHP 版本:5.5.12

主机版本:

Apache 版本:Apache/2.2.27

PHP 版本:5.6.14

如果问题是版本问题,那么有什么可能的解决方案让它在托管版本中工作?

【问题讨论】:

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


    【解决方案1】:

    这样试试吧,

    Options -MultiViews
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^sections?$ sections.php [NC]
    RewriteRule ^sections/?$ sections.php [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^sections/([0-9]+)/([a-zA-Z-.]+)/view?$ view_sections.php?id=$1&name=$2 [QSA,NC]
    RewriteRule ^sections/([0-9]+)/([a-zA-Z-.]+)/view/?$ view_sections.php?id=$1&name=$2 [QSA,NC,L]
    

    【讨论】:

    • 谢谢你!有效!我刚刚在上面插入了这个(选项-MultiViews)。顺便问一下,你能告诉我那条线的用途吗?呵呵
    • 嗯,这里解释的很好stackoverflow.com/questions/25423141/…
    • 如果它对您有用,您可以通过单击答案旁边的向右箭头来确认这是一个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多