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

标签 apache .htaccess mod-rewrite

我有一个包含查询字符串变量的当前 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/$1.php -f
RewriteRule ^(.+?)/?$ access/$1.php [L,NC]

网站 map

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/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ access/$1.php?post=$2&tab=$3 [QSA,L]

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

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

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

关于apache - 重写网站 URL 以删除查询字符串变量问号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66850720/

相关文章:

zend-framework - 使用 SSL 和 MAMP 的 mod_rewrite 出现 404 错误

apache - Htaccess : URL Rewriting not causing 404 on nonexisting pages and not working as expected

java - Apache/Tomcat 上的 Log4j 不工作

linux - 在网络服务器上备份文件!和〜

php - 阻止不需要的访客 : htaccess, iptables,或者只是数据库

apache - 防止在没有 .htaccess 的情况下从 Apache 访问文件

php - 我没有获得所需的 URLRewrite

php - 用于部署的语言/堆栈

macos - httpd : Could not reliably determine the server's fully qualified domain name

php - curl: (60) SSL 证书:无法获取本地颁发者证书