.htaccess 用问号重写 URL

Posted

技术标签:

【中文标题】.htaccess 用问号重写 URL【英文标题】:.htaccess rewrite URL with question mark 【发布时间】:2021-08-21 16:29:30 【问题描述】:

我有一个名为“Post Affiliate Pro”的程序给我的 htaccess 代码,看起来像这样

# Start Post Affiliate SEO Code----

RewriteEngine On

RewriteCond %REQUEST_FILENAME -f [OR]

RewriteCond %REQUEST_FILENAME -d

RewriteRule .? - [S=4]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2&chan=$3 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2&chan=$3&data1=$4 [R=301,L]

RewriteRule ^?coupon-code=([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=$1&a_bid=$2&chan=$3&data1=$4&data2=$5 [R=301,L]

# End of Post Affiliate SEO Code

这会破坏我的网站,因为我尝试重定向的 URL 开头有一个查询字符串,例如 https://example.com/?coupon-code=PGZ

我知道我需要以某种方式使用 QUERYSTRING 对其进行修改,但我不知道具体该怎么做。谁能帮我修改这个 htaccess 代码,以免破坏我的网站?

【问题讨论】:

【参考方案1】:

根据您显示的尝试/示例,请尝试在您的 .htaccess 文件中遵循规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

##Making RewriteEngine ON here.
RewriteEngine On
##For existing pages rules here..
RewriteCond %REQUEST_FILENAME -f [OR]
RewriteCond %REQUEST_FILENAME -d
RewriteRule .? - [S=4]

##For handling 4 parameters handling rules here.
RewriteCond %QUERY_STRING ^coupon-code=([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2&chan=%3&data1=%4 [R=301,L]

##For handling 3 parameters handling rules here.    
RewriteCond %QUERY_STRING ^coupon-code=([\w-]+)/([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2&chan=%3 [R=301,L]

##For handling 2 parameters handling rules here.
RewriteCond %QUERY_STRING ^coupon-code=([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2 [R=301,L]

##For handling 1 parameters handling rules here.
RewriteCond %QUERY_STRING ^coupon-code=([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1 [R=301,L]
    
##For handling 5 parameters handling rules here.
RewriteCond %QUERY_STRING ^coupon-code=([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ [NC]
RewriteRule ^/?$ https://example.postaffiliatepro.com/scripts/l5zhexygnc?a_aid=%1&a_bid=%2&chan=%3&data1=%4&data2=%5 [R=301,L]

# End of Post Affiliate SEO Code

【讨论】:

以上是关于.htaccess 用问号重写 URL的主要内容,如果未能解决你的问题,请参考以下文章

Codeigniter重写URL用斜杠替换问号

PHP中查询字符串的HTACCESS重写规则

.htaccess 用两个变量重写 url

PHP/Apache:用 .htaccess 重写规则

htaccess 用最终的子文件夹重写 url

用.htaccess文件实现URL重写