删除.php扩展名并替换/带问号

时间:2016-08-02 12:17:31

标签: php .htaccess mod-rewrite

我需要使用以下网址

http://localhost/gbm/file/services

并将其发送至

http://localhost/gbm/file.php?param=services

我现在使用.htaccess中的代码,只是删除.php扩展名但不替换/并将其他字符串添加到id=

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

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

## hide .php extension snippet

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

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

# To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule ^gbm/(\w+)/(.*) /gbm/$1.php/?param=$2

1 个答案:

答案 0 :(得分:1)

您可以使用:

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

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

# To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{THE_REQUEST} /(\w+)\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]

## hide .php extension snippet

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

# handle ?param=...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/gbm/$1\.php -f
RewriteRule ^(.+)/([\w-]+)/?$ $1.php?param=$2 [L,QSA]


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