4

不幸的是,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变量可以是logincreate等。帐户目录中的 将处理此变量dashboardlogoutindex.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

4

5 回答 5

2

我在本地尝试过,无法重现。该帐户工作正常。

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

  • 按照Ravi Thapliyal的回答。

其他点很明显:

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

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

于 2013-05-26T19:58:10.220 回答
2

用 RewriteRule 中的主题目录替换帐户。

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

.htaccess如果这可行,请检查帐户目录下是否有其他干扰。如果您无法找到任何其他文件,还请检查httpd.conf(在<Directory>标签下)中是否存在任何冲突的 RewriteRule。

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

RewriteRule ^account/([a-zA-Z0-9]+)/$ account/index.php?page=$1 [L]
于 2013-05-25T23:38:01.193 回答
0

我可以在您的 .htaccess 中看到一些错误,例如:

  1. 使用RewriteCond %{REQUEST_FILENAME} !-fandRewriteCond %{REQUEST_FILENAME} !-d仅用于themes规则但跳过account规则。
  2. L不以(Last) 标志结束重写规则。
  3. 不使用 QSA(查询字符串附加)标志来保留查询字符串。

这是您应该尝试的修改后的 .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]
于 2013-05-31T04:48:26.633 回答
0

目前,由于连字符,您没有匹配操作页面,添加连字符作为匹配字符集的一部分将解决此问题

RewriteRule ^account/([a-zA-Z0-9-]+)/$ account/index.php?page=$1
于 2013-05-21T14:11:36.293 回答
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

于 2013-05-26T12:19:33.867 回答