重写网站 URL 以删除查询字符串变量问号?

Rewrite website URL to remove query string variables question mark?

我有一个包含查询字符串变量的当前 url。我想删除它们,以便只有查询字符串的第二部分可见。
来自:mysite.com/posts?post=45&tab=profile
收件人:mysite.com/posts/45/profile
当前 .htaccess 个文件

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+access/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/access/.php -f
RewriteRule ^(.+?)/?$ access/.php [L,NC]

站点地图

index.php
/access
    -dashboard.php
    -posts.php
    -account.php

此查询字符串将始终包含参数 1,但可能包含也可能不包含参数 2,具体取决于他们在屏幕上单击的内容。

像这样:

Options -MultiViews
RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+access/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 2 parameter rule
RewriteCond %{DOCUMENT_ROOT}/access/.php -f
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ access/.php?post=&tab= [QSA,L]

# 1 parameter rule
RewriteCond %{DOCUMENT_ROOT}/access/.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ access/.php?post= [QSA,L]

# php extension
RewriteCond %{DOCUMENT_ROOT}/access/.php -f
RewriteRule ^(.+?)/?$ access/.php [L,NC]

# php extension
RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.+?)/?$ .php [L,NC]
  • 选项 MultiViews(参见 http://httpd.apache.org/docs/2.4/content-negotiation.html)由 Apache's content negotiation module 使用,它在 之前 mod_rewrite 运行并使 Apache 服务器匹配文件的扩展名。因此,如果 /file 是 URL 那么 Apache 将提供 /file.html.