.htaccess - 使用 htaccess 创建漂亮的链接 - 在我的非空 .htaccess
问题描述
所以现在我只是想转换像
https://gregorj.org/post?id=26
至
https://gregorj.org/post/26
我发现的所有东西(也在stackoverflow上)告诉我把它放在.htaccess中:
RewriteEngine On
RewriteRule ^post/([^/.]+)?$ /post?id=$1 [L]
但在我的情况下它不起作用。我假设这可能只是因为我的 .htaccess 中有一堆其他的东西,我把它们放在那里是为了在我启动网站时隐藏 .php 和 .html 扩展名(这也是我写 /post?id 的原因=$1 而不是 .htaccess 中的 /post.php?id=$1 - 但我都试过了,以防万一:)。
这是我今天的 .htaccess :
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R,L]
# Prevent POST requests from getting redirected
RewriteCond %{REQUEST_METHOD} !POST
# 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 [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
关于我可以做些什么来获得更漂亮的链接的任何想法?
解决方案
好吧,我回答了我自己的问题。
如果我开始阻止 POST 请求被重定向,那么 ^post 可能不是启动重定向规则的好方法......
我把它改成了 ^article 没有问题