My website do not use any GET parameters except on one page. Nonetheless, I can see that Google managed to index a bunch of my pages with GET parameters. This is not great for SEO (duplicate content)...
So I'm trying to edit my .htaccess to do 301 redirects between all urls with GET parameters to url without GET parameters (except for one url). Some examples:
example.com/?foo=42 => example.com/
example.com/about?bar=42 => example.com/about
example.com/r.php?foobar=42 => the url r.php should keep the GET parameters
So far I'm trying to remove all GET parameters, and it doesn't work.
RewriteEngine On
RewriteRule ^(.*)\?(.*)$ http://www.example.com/$1 [L,NC,R=301]
Any idea how to fix that?
You cannot match query string using RewriteRule.
You can use this generic rule to remove all query string except for requests that have DOT:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^([^.]*)$ /$1? [L,NE,R=301]
Related
This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive
You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.
Need Guidance of 301 redirecting Url through htaccess matching specific format
Existing URL
http://www.example.com/index.php?option=com_toys&limitstart=20
Proposed URL
http://www.example.com/index.php?option=com_toys&view=list&Itemid=2&limitstart=20
Here limitstart may change to 0,20,40,60 to even 1000 and thus should remain same in the new proposed url too
Can anyone advise on to redirect using htaccess of above
You need to match query string using RewriteCond using a regex to capture both parameters:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(option=com_toys)&(limitstart=\d+)$ [NC]
RewriteRule ^/?index\.php$ %{REQUEST_URI}?%1&view=list&Itemid=2&%2 [L,NC,R=301]
I have a few url that have appeared in google search console with "?" and under scores "_"in the URL. I have tried many ways to redirect them but i have failed. I believe it has some thing to do with using the %{QUERY_STRING}
this is the URL that i need redirecting from
repairs-blog?journal_blog_tag=iPhone
to
blog?journal_blog_tag=iPhone
Maybe someone could write the solution?
When a query string is involved, you use RewriteCond with %{QUERY_STRING}, e.g.
RewriteCond %{QUERY_STRING} journal_blog_tag=
and then rewrite with RewriteRule
RewriteRule ^repairs-blog$ /blog [L]
Because you don't change the query string, there's nothing more to do.
When you want to rewrite this URL, no matter what query string is given, you can also omit the RewriteCond and just use the RewriteRule.
If you want to redirect instead of rewrite, e.g. change the URL in the client, you add the R flag
RewriteRule ^repairs-blog$ /blog [R,L]
Excuse me for my english.
I make a brands directory web site.
Before to acces to the brands pages I use requests like this :
mydomain.com/fiche.php?id=115
where id is the id of the brand in my directory
I change the structure of the brands pages and now use this request:
mydomain.com/annuaire.php?type=fiche&id_marq=115
where id has become id_marq
I try to use a rewritebrule like this:
RewriteRule ^fiche.php$ http://www.annuaire-sites-officiels.com/annuaire.php?detail=fiche&id_marq=$1 [L,QSA,R=301]
to redirect the old links to the new pages but result dont pass the id_marq value and the url is:
http://www.annuaire-sites-officiels.com/annuaire.php?detail=fiche&id_marq=&id=115
&id= is too.
What am I doing wrong?
Your rule is not evaluating query string and that's why its not capturing id query parameter.
Change your code to:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^fiche\.php$ /annuaire.php?detail=fiche&id_marq=%1 [R=302,L,QSA,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Check out Regex Back Reference Availability:
You have to capture the query string. [QSA] passes it forward unaltered, so unless you're using id for anything you don't need that bit of code. Your 301 redirect is correct since this is a permanent redirect. Remember if you add a failed redirect your browser may cache that redirect so it might not look like it's working.
In this string match I'm only catching numbers to prevent someone from passing something like an asterisk * and XSS exploiting your site.
I've not included and [NC] matches in my code because when you allow multiple cases they can seem like different URLs to search engines (bad for SEO).
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^fiche.php$ http://%{HTTP_HOST}/annuaire.php?detail=fiche&id_marq=%1 [R=301,L]
I've removed and/or combined a couple of pages on a site, And now I need to set up a 301 redirect.
I thougt doing so in my .htaccess was my best bet, but the rules I trying to add doesen't get noticed or something. They don't respond at all...
These are the rules I've tried so far:
Redirect 301 /?Page=sPage&sPage=Our-Store %{SERVER_NAME}?Page=sPage&sPage=About-Us
RewriteRule ^/?Page=sPage&sPage=Our-Store$ %{SERVER_NAME}?Page=sPage&sPage=About-Us[R=301,NC,L]
RewriteCond %{HTTP_HOST} !^%{SERVER_NAME}$ [NC]
RewriteRule . %{SERVER_NAME}%{REQUEST_URI}?Page=sPage&sPage=About-Us [R=301,L]
This last one messed up the CSS and JS src's...
I have this at the top:
RewriteEngine On
RewriteBase /
Any suggestion?
UPDATE : follow up question
I have like 3000+ equal url strings with an ending ID that is different. How do I redirect all those requests?
This is the old url : ?Page=Tuninglist&Car=*
And this is the new one : ?Page=Tuning&view=vehicle&type=Car&id=*
* The value of id= is just integers...
Was hoping something like this could work, but no - got a 500 server error instead...
RewriteCond %{QUERY_STRING} ^Page=Tuninglist&Car=([0-9]+)$
RewriteRule ^ ?Page=Tuning&view=vehicle&type=Car&id=$1 [R=301,L]
*EDIT: The 500 server error occurred because I had a ? at the beginning of the condition.
The redirect now works, but the ending id value doesn't get included.
All I get is the correct page, but not the associated content based on that id...
You can't match against the query string in a redirect or rewrite rule, you need to do it using the %{QUERY_STRING} variable in a condition:
RewriteCond %{QUERY_STRING} ^Page=sPage&sPage=Our-Store$
RewriteRule ^ %{REQUEST_URI}?Page=sPage&sPage=About-Us [R=301,L]