.htaccess网址可以像这样重定向吗?

时间:2015-02-27 08:05:37

标签: regex apache .htaccess mod-rewrite redirect

我设置了.htaccess,它非常适用于以下链接:

http://localhost:8888/z-jquery/view
http://localhost:8888/z-jquery/search

所以它正在删除.php扩展名,如果他们手动输入也会重定向,这是完美的,但我也需要我的其他网址:

http://localhost:8888/z-jquery/edit?client=dane+kasbo
如果可能,

就是这样:

http://localhost:8888/z-jquery/dane+kasbo

这是我的.htaccess文件:

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

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

注意:我在这里搜索了很多.htaccess问题,但没有一个对我有用,任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

您可以使用:

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

RewriteCond %{THE_REQUEST} /edit(?:\.php)?\?client=([^&]+)\s [NC]
RewriteRule ^ %1? [R,L,NE]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([^/.]+)/?$ edit.php?client=$1 [L,QSA]