使用PHP和.htaccess重写URL

时间:2015-11-29 12:47:30

标签: .htaccess

大家好我是.htaccess网址重写的新用户,我有像

这样的网址
  

http://localhost/pune99/Movie/xyz+pvt+ltd

但我希望它像这样

  

http://localhost/pune99/xyz-pvt-ltd

提前感谢...

  

我的.htaccess代码

#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# executes repeatedly as long as there are more than 1 spaces in URI
RewriteRule "^(\S*)\s+(\S*\s.*)$" /$1-$2 [L,NE]

# executes when there is exactly 1 space in URI
RewriteRule "^(\S*)\s(\S*)$" /$1-$2 [L,R=302,NE]

#RewriteBase /

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

## hide .php extension snippet

# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)?$ $1.php?name=$2 [L,QSA

1 个答案:

答案 0 :(得分:1)

将前2个规则更改为此规则,以便将+和空格转换为连字符:

# executes repeatedly as long as there are more than 1 spaces in URI
RewriteRule "^([^\s+]*)[\s+]+([^\s+]*[\s+].*)$" /$1-$2 [L,NE]

# executes when there is exactly 1 space in URI
RewriteRule "^([^\s+]*)[\s+]+([^\s+]*)$" /$1-$2 [L,R=302,NE]