htaccess - URL使用第二个参数时重写

时间:2015-06-04 21:42:48

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

我已经完成并检查了其他有问题的问题,而且无法找到我在任何地方寻找的东西。以下是我迄今为止尝试过的选项......

answerthis onethis one

我现在的问题是,当我转到example.com/account/时,会转到404找不到的页面。但是,当我转到example.com/account/1/时,它会按预期加载页面。下面是我目前的htaccess。我尝试过各种各样的变化,似乎没什么用。

# .htaccess file

RewriteEngine on
Options -MultiViews +FollowSymLinks

# Rerwite the domain

RewriteCond %{HTTP_HOST} !^example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# Rewrite conditions for each page type

RewriteCond /%{REQUEST_FILENAME} !-d
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9-]+)/$ /index.php?page=$1 [L]

RewriteCond /%{REQUEST_FILENAME} !-d
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/$ /index.php?page=$1&id=$2 [L]

# Disallow access to both this file and the config file

<Files .htaccess>
order allow,deny
deny from all
</Files>

<Files config.php>
order allow,deny
deny from all
</Files>

# Various error pages
ErrorDocument 300 /error/
ErrorDocument 400 /error/
ErrorDocument 403 /error/
ErrorDocument 404 /error/
ErrorDocument 408 /error/
ErrorDocument 500 /error/
ErrorDocument 503 /error/

正如笔记一样,我已编辑主域名重写为example.com,因为此网站正在开发中且无法公开。所以忽略那一点。

2 个答案:

答案 0 :(得分:1)

你的规则如下:

RewriteEngine on
Options -MultiViews +FollowSymLinks
# Rerwite the domain

RewriteCond %{HTTP_HOST} !^example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

# Rewrite conditions for each page type

# skip all files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

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

RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /index.php?page=$1&id=$2 [L,QSA]

答案 1 :(得分:1)

我不会在变量前添加斜杠。这对我来说很好。

Options -MultiViews 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /index.php?page=$1&id=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-]+)/?$ /index.php?page=$1 [L]