htaccess - 一个干净的URL工作但不是另一个

时间:2013-05-21 14:02:20

标签: .htaccess mod-rewrite clean-urls pretty-urls

不幸的是,htaccess文件从来都不是我的强项。

我会直接进入:

#Turn RewriteEngine on
    Options +FollowSymLinks
    RewriteEngine on


#Canonicalize URL
    RewriteCond %{HTTP_HOST} !^www\.domain-removed\.com [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/?(.*) http://www.domain-removed.com/$1 [L,R,NE]


#Add Trailing Slash
    RewriteCond %{REQUEST_URI} !(/$|\.) 
    RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]


#Clean URLs
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^themes/([a-zA-Z0-9]+)/$ themes/view.php?theme=$1
    RewriteRule ^account/([a-zA-Z0-9]+)/$ account/index.php?page=$1

所以转到domain.com/themes/theme-name/domain.com/themes/view.php?theme=theme-name工作正常,两者都显示相同的页面/结果。但是,转到domain.com/account/index.php?page=page无效,但转到domain.com/account/page/却没有,它只返回404 Not Found。

page变量可以是logincreatedashboardlogout等内容。帐户目录中的index.php将是处理这个变量。

令我困惑的是,为什么它不起作用,因为它是相同的情况,只是在不同的目录和不同的文件名下,但两者都在规则中声明。现在任何人我做错了什么?

修改

我也尝试过这样做并定义页面,如下所示:

    RewriteRule ^account/login/$ account/login.php
    RewriteRule ^account/logout/$ account/logout.php
    RewriteRule ^account/create/$ account/create.php
    RewriteRule ^account/dashboard/$ account/dashboard.php

但它仍然只返回404

5 个答案:

答案 0 :(得分:2)

将帐号替换为RewriteRule中的themes目录。

RewriteRule ^account/([a-zA-Z0-9]+)/$ themes/view.php?theme=$1

如果有效,请检查帐户目录下是否有其他.htaccess干扰此处。如果您无法找到任何其他文件,请检查httpd.conf中的任何有冲突的RewriteRule(<Directory>标记下)。

如果没有给出,请将您的规则标记为[L] ast,即mod_rewrite应停止再处理它。

RewriteRule ^account/([a-zA-Z0-9]+)/$ account/index.php?page=$1 [L]

答案 1 :(得分:2)

我在本地试了一下,无法重现它。该帐户工作正常。

  • 我在重定向到domain-removed.com时出现问题。您确定要将所有请求重定向到此域吗?

  • 按照 Ravi Thapliyal 的回答。

其他一点显而易见:

  • 请确保拼写折叠'帐户'的名称和index.php正确

  • 尝试RewriteRule ^account/login/$ account/login.php时,请确保login.php存在。

答案 2 :(得分:0)

目前你因为炒作而没有匹配动作页面,添加连字符作为匹配字符集的一部分将解决此问题

RewriteRule ^account/([a-zA-Z0-9-]+)/$ account/index.php?page=$1

答案 3 :(得分:0)

尝试将[L,PT]添加到RewriteRule指令。

RewriteRule ^themes/([a-zA-Z0-9]+)/$ themes/view.php?theme=$1 [L,PT]
RewriteRule ^account/([a-zA-Z0-9]+)/$ account/index.php?page=$1 [L,PT]

参考:http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l

答案 4 :(得分:0)

我可以在你的.htaccess中看到一些错误:

  1. 仅针对RewriteCond %{REQUEST_FILENAME} !-f规则使用RewriteCond %{REQUEST_FILENAME} !-dthemes,但跳过account规则。
  2. 没有结束用L(最后)标志重写规则。
  3. 不使用QSA(查询字符串追加)标志来保留查询字符串。
  4. 以下是您应该尝试修改的.htaccess:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    # force www.domain-removed.com domain
    RewriteCond %{HTTP_HOST} !^www\.domain-removed\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain-removed.com/$1 [L,R=302,NE]
    
    # add trailing slash
    RewriteRule (?!^(?:.*?/|.+?\..*)$)^.*$ %{REQUEST_URI}/ [R=302,L]
    
    ## Clean URLs  
    
    # do nothing for real files, directories or sym-links
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f [OR]
    RewriteCond %{SCRIPT_FILENAME} -l
    RewriteRule - [L]
    
    # no handle /themes/ and /account/ URIs
    RewriteRule ^themes/([a-zA-Z0-9]+)/?$ /themes/view.php?theme=$1 [L,QSA]
    
    RewriteRule ^account/([a-zA-Z0-9]+)/?$ /account/index.php?page=$1 [L,QSA]