微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为 GET 请求正确配置 Apache 重写规则?

如何解决如何为 GET 请求正确配置 Apache 重写规则?

当我尝试创建用户友好的 URL 时遇到以下问题,它返回 500 错误

不起作用

RewriteEngine On
RewriteRule  ^page/(.+)$   /page.PHP?type=$1   [L]

仅当我更改脚本名称时才有效

RewriteRule  ^page/(.+)$   /change-page.PHP?type=$1   [L]

有什么办法可以保留重定向页面的 page.PHP 吗?谢谢

这里是完整的 .htaccess 配置

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

# For LocalHost !.PHP
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1

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

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

RewriteRule  ^page/(.+)$  /page.PHP?type=$1   [L]

解决方法

试试这些规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[?\s] [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([\w-]+)/?$ page.php?type=$1 [L,QSA,NC]

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

,

您应该尝试遵循 .htaccess 文件中的规则。在测试您的网址之前,请务必清除浏览器缓存。

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

# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1

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


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


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([\w-]+)/?$ page.php?type=$1 [L,NC]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。