.htaccess删除扩展名并获取查询参数

pu3pd22g  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(157)

我的.htaccess是这样的:

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

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

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

我想添加,也删除PHP扩展到页面,另一个东西.重定向查询.例子我有这个页面:

https://mywebsite.it/room?id=1

对外我想这样表示:

https://mywebsite.it/room/1

但在内部需要始终重定向到room?id=1,因此我可以使用$_GET
谢谢你的帮助

dgjrabp2

dgjrabp21#

有这样的说法:

Options -MultiViews
RewriteEngine On

## external redirect from /room?id=123 to /room/123 URL
RewriteCond %{THE_REQUEST} \s/+\(room)(?:\.php)?\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=302,NE,L]

## To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

## internal rewrite to from /room/111 to room.php?id=1.php
RewriteRule ^(room)/(\d+)/?$ $1.php?id=$2 [QSA,NC,L]

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

验证它工作正常后,将R=302替换为R=301。测试mod_rewrite规则时,避免使用R=301(永久重定向)。

相关问题