在文件夹中重写参数.htaccess

时间:2014-01-12 20:12:51

标签: .htaccess

我想要网址

domain.com/user/user.php?id=sam

domain.com/user/sam

我已经在user文件夹之外有了以下.htaccess ..

Options -MultiViews 
RewriteEngine On 
RewriteBase /

RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" 
RewriteRule ^(.*)\.php(\?.*)?$ $1$2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ $1.php [L]

我尝试了很多解决方案,除了这个(放在用户文件夹中)之外没有用到

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ user/user.php?id=$1 
RewriteRule ^([a-zA-Z0-9_-]+)/$ user/user.php?id=$1

但这样做的目的是将网址重定向到domain.com/user/?id=

我该如何解决?

更新:

Options -MultiViews 
RewriteEngine On 

ErrorDocument 404 404.shtml
ErrorDocument 401 401.shtml
ErrorDocument 403 403.shtml
ErrorDocument 400 400.shtml
ErrorDocument 500 500.shtml

## don't touch admin/yshout/ URIs 
RewriteRule ^admin/yshout/ - [L,NC]

## don't touch controller/ URIs 
RewriteRule ^controller/ - [L,NC]

## don't touch server/ URIs 
RewriteRule ^server/ - [L,NC]

RewriteCond %{THE_REQUEST} \ /+user/user\.php\?id=([^&\ ]+) 
RewriteRule ^ /user/%1? [L,R=301]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?id=$1 [L]

RewriteEngine On 
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" 
RewriteRule ^(.*)\.php(\?.*)?$ $1$2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ $1.php [L]

1 个答案:

答案 0 :(得分:0)

您无法在重写规则中匹配查询字符串。尝试:

RewriteCond %{THE_REQUEST} \ /+user/user\.php\?id=([^&\ ]+)
RewriteRule ^ /user/%1? [L,R=301]

并确保它是第一条规则,然后将其添加到它下方:

RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?id=$1 [L]

您也不想创建重写基础/,您可以删除该行或将其更改为/user/