.htaccess redirect non existing pages based on index.php page with wildcard and something after it to index.php

走远了吗. 提交于 2019-12-24 07:31:50

问题


Please help, some website is arbitrarily adding dynamic query strings to my home page.

For example my home page is www.mysite.com/index.php and they link to many links like this:

www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing

And those links are opening on my site, content of page are the same for every page, just like my original www.mysite.com/index.php

There are few hundreds of that links pointing to my site. So how I can redirect this:

www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
...

to

www.mysite.com/index.php or just to www.mysite.com/

This what I tried so far in my .htaccess file

RedirectMatch 302 ^index.php?a= http://www.www.example-ste.com/
RewriteRule ^/index.php?a=(.*) http://www.example-ste.com/

But still pages are opening on site.


Another similar question.

How to redirect pages ending with "?pagewanted=all" to the same page but with out that "?pagewanted=all"

For example I need to redirect page:

www.mysite.com/something-something/something.html?pagewanted=all

to

www.mysite.com/something-something/something.html


Hello.

I just noticed something. I needed URL redirection rule which will redirect pages like:

www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing

to home page of site, root. And you gave me this code:

RewriteEngine On
RewriteBase /

#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .*  http://www.mysite.com/? [R=301,L]

And I must say it works fine, it does that, but I just noticed that it somehow blocks or redirect all links containing ?a= for example on some temporary pages I have links like:

i.php?a=something-something-something

So, can you adopt code just for pages based on index.php like:

www.mysite.com/index.php?a=something-something-something

and not for links with:

i.php?a=something-something-something

If I am right it works on all links with "a=" but I need just for "index.php?a="


回答1:


Try adding the folloginw to the top of your .htaccess file

RewriteEngine On
RewriteBase /

#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .*  http://www.mysite.com/? [R=301,L]

#if the query string has a pagewanted parameter
RewriteCond %{QUERY_STRING} (^|&)pagewanted=all [NC]
#Redirect and remove query string parameters for html pages
RewriteRule (.+\.html)  http://www.mysite.com/$1? [R=301,L]

Edit. Added rule to remove pagewanted=all



来源:https://stackoverflow.com/questions/8535587/htaccess-redirect-non-existing-pages-based-on-index-php-page-with-wildcard-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!