使用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扩展名(这也是为什么我在.htasccess中写/post?id=$1而不是/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可能不是启动重定向规则的好方法。。。

我把它改成^文章,没有问题

最新更新