apache - .htaccess 重写带有问号的 URL

标签 apache .htaccess redirect mod-rewrite query-string

我有一个名为“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 修改它,但我不知 Prop 体要做什么。谁能帮我修改这个 htaccess 代码,以免它破坏我的网站?

最佳答案

根据您显示的尝试/示例,请尝试遵循 .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

关于apache - .htaccess 重写带有问号的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67823326/

相关文章:

Eclipse 及其如何处理 JARS —— 奇怪的情况

apache - 无法启用 php display_errors

apache - 符号链接(symbolic link)到/project/current/web work 但不是/project/current/web/app.php (Apache/Ubuntu/Symfony2)

apache - mod_expires 为 3## 状态码发送 Cache-Control header

apache - 为什么Apache指向/var/www?

php sleep 功能奇怪的行为

php - 如何强制 SSL WWW 到我的域和子域

linux - 映射 2 个目录 linux-server

python - 我如何使用 Django 拥有多个导航路径,例如简化的向导路径和完整路径?

regex - 减少链接的 URL 重定向 [IIS/Regex/ReWrite]