Redirecting (301) multiple urls with the same query string using .htaccess - apache

I wonder if anyone can help. I've researched this issue but not found a post that QUITE matches it so I'm a bit lost as to how to proceed.
I have a whole bunch of URLs from an old site that each use the same query string, which is used to redirect users to the mobile version of the pages. I would like to redirect these URLs to pages on the new site so that the new URLs don't display the query string.
So:
www.old-site.com/first-page/?type=88 should redirect to www.new-site.com/first-page;
www.old-site.com/second-page/?type=88 should redirect to www.new-site.com/second-page; and
www.old-site.com/third-page/?type=88 should redirect to www.new-site.com/third-page, etc.
I've tracked down code that will redirect one URL with a query string to another URL without a query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^type=88
RewriteRule (.*)$ http://www.new-site.com? [R=301,L,NC]
but, I am not sure how to deal with all these different pages that use the same query string, but each need to end up at different destinations.
Any help that anyone can provide would be most helpful.
With kind regards,
Mark

Try :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^type=88
RewriteRule (.*)$ http://www.new-site.com%{REQUEST_URI}? [R=301,L,NC]

Related

Redirect omitting parameters in .htaccess

After a couple of hours of fighting, I manage to generate an htacces file to redirect all my old URLs to the URLs of my new site (same domain). I am using the following rules to manage my subfolders redirects (my new site has only a couple of pages):
RewriteRule ^es/empresa/terminos-y-condiciones.html$ http://domain/terminos-y-condiciones.html [L]
RewriteRule ^es/servicios/registro-de-dominios/.*$ http://domain/dominios.html [R=301,NC,L]
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L]
RewriteRule ^es/component/.*$ http://domain/contacto.html [R=301,NC,L]
RewriteRule ^es(/.*)?$ / [R=301,NC,L]
The problem is that when I try to access to some pages like domain.com/empresa/testimonios-de-clientesc69a.html?lang=es it redirects me to domain/?lang=es , that is my domain+the parameter after ".html".
So, I was wondering if there was a way to eliminate this parameter and redirect only to www.domain.com ?
Thanks in advance!
To truncate a query string add a '?' sign to the end of target URLs like this:
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html? [R=301,NC,L]
On Apache 2.4 or later you can use the QSD flag to discard the query string (see documentation):
When the requested URI contains a query string, and the target URI
does not, the default behavior of RewriteRule is to copy that query
string to the target URI. Using the [QSD] flag causes the query string
to be discarded.
So it would look like
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L,QSD]

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

How to create RewriteRule where the request URL has query string

I feel like I'm really close, but I can't quite get this Apache RewriteRule to work correctly.
I have a URL like http://mysite.com/product.php?view=true&ID=123 and I would like to redirect to http://mysite.com/some-page.But if the product ID=456, then I'd like to redirect it to http://mysite.com/some-other-page.
So I don't need to keep the query string for my destination URLs, but I do care what it equals in the request because that will determine where I redirect the user to. I've been struggling with trying to understand how to use the mod_rewrite %{QUERY_STRING} parameter, but I just can't get it to work.
Thanks in advance for your help!
This should help a little bit.
RewriteCond %{QUERY_STIRNG} id=456
RewriteRule .* /some-other-page? [R,L]
More about Manipulating the Query String.
Like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} =view=true&ID=123
RewriteRule ^product\.php$ /some-page? [R=301,L]
RewriteCond %{QUERY_STRING} =view=true&ID=456
RewriteRule ^product\.php$ /some-other-page? [R=301,L]
You need to match ^product.php as well as query string. I did match FULL EXACT query string (both view=true and ID=123 need to be present in EXACT order). If you need only partial match (e.g. ID=123 only), then replace =view=true&ID=123 by ID=123.
Notice ? at the end of new URL -- this is to get rid of existing query string. Without it /product.php?view=true&ID=123 will be redirected as /some-page?view=true&ID=123.
I've used 301 Permanent Redirect. You may want to change it to 302 or whatever other redirect code you think is better for you.
This is to be placed in .htaccess in in website root folder. If placed elsewhere some small tweaking may be required.

301 redirect query string to SEO friendly URLs through .htaccess

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
So that the ugly
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
Is now beautiful
http://www.mysite.com/directory/seo/striped/
Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.
The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.
Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!
This should do the trick:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N
Hope this helps.
Not tested, but this may work...
RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

Apache Mod_Rewrite query string to standard url

I was using a .htaccess redirect 301 to redirect a URL which then appended all the query string elements to the end, leaving me with some URLs indexed in Google as /store/product/?d=department&s=section&p=product.
I have fixed the redirect by using a RewriteRule instead which doesn't append the query strings, however I'm stuck trying to rewrite the old redirected URLs with the query strings back to the original URLs (as these are looking like two different URLs to Google now).
I have managed to get a RewriteRule to sort of work, in that /store/product/xxxxx redirects to /store/product/ as it should, it just doesn't seem to work with the whole query string of.
What I have been using is:
RewriteRule ^store/product/([a-zA-Z0-9\-_\?=&]+)$ http://www.example.com/store/product/ [NC,R=301,L]
or
RewriteRule ^store/product/\?d=department&s=section&p=product$ http://www.example.com/store/product/ [NC,R=301,L]
Hope that all makes sense!
Many thanks
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^store/product/.*$ http://www.example.com/store/product/? [NC,R=301,L]
You need to specify an empty query in your substitution to not have the original one automatically appended to the new URL:
RewriteRule ^store/product/[a-zA-Z0-9\-_=&]+$ http://www.example.com/store/product/? [NC,R=301,L]
Note the ? at the end of the substitution URL.