.htaccess重定向,url中没有扩展名

lpwwtiir  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(136)

我正在隐藏文件扩展名

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

导航到此链接时http://localhost/website/profile?user=user01&nofollow=1
我被重定向到这里http://example.com/folder/profile?user=user01
我已经把.php从url中去掉了。为什么它会重定向我?

ctehm74n

ctehm74n1#

/website/.htaccess中这样设置:

RewriteEngine On
RewriteBase /website/

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php[?\s] [NC]
RewriteRule ^(.+)\.php$ $1 [R=301,L,NC]

# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]

请确保在测试之前清除浏览器缓存。

vc9ivgsu

vc9ivgsu2#

请尝试使用以下方法:

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]

相关问题