htaccess重定向规则,不影响现有目录

时间:2013-04-04 10:23:55

标签: .htaccess

我需要一个重定向的.htaccess规则:http://www.mysite.com/ {username} / {id}

至:http://www.mysite.com/video.php?username= {username}& id = {id}

难点在于它不应该影响现有的目录,例如images / script / admin / js / music / etc ......

http://www.mysite.com/images/logo.jpeg不应受此规则的影响)

有任何线索吗?

2 个答案:

答案 0 :(得分:1)

尝试一下,将此代码用于您网站的.htaccess文件:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /video.php?username=%1&id=%2 [R]

现在,如果你想重写而不是重定向?只需删除[R]标记:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /video.php?username=%1&id=%2

答案 1 :(得分:0)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /video.php?username=$1&id=$2 [L,QSA]