使用htaccess(url重写)在php中的自定义URL

时间:2016-02-04 07:05:45

标签: .htaccess

我是php htaccess的url重写新手。目前我的网址如同http://localhost/htaccess/Movie/my-parameter-value

但我希望http://localhost/htaccess/my-parameter-value我想删除电影(电影来自动作源名称ex" movie.php");
提前致谢

这是我的代码:

#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On     
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]

# 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)

有这样的话:

#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On     
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]

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

# /htaccess/Movie.php?name=movie-name => /htaccess/movie-name
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]

# handle /htaccess/movie-name internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ Movie.php?name=$1 [L,QSA]