modrewrite redirects for URLs with arguments - .htaccess

Question about 301 redirect / modrewrite
I know there are a lot of topics on this subject but so far I have not managed to create my needed redirect.
here is my current URL : mysite.net/doc.php?title=john
needs to redirect to : mysite.net/doc.php?renamed=john&id=3340
so I am adding 'id' and renaming the variable 'title'.
Does anyone know if this can actually be done ?

Use following
"/docs.php?title=^([^/]+)" "http://new.example.com/docs/renamed=$1&id=3340" [L]
Rewriting For Certain Query Strings
Rewrite URLs like http://example.com/page1?title=val to http://example.com/page2?var=val but don't rewrite if val isn't present.
RewriteCond %{QUERY_STRING} val
RewriteRule ^/page1 /page2?renamed=$1&id=3340

Related

Redirect all urls which contain certain parameters to another url which follows a certain pattern

Unfortunately I didn't get it solved by myself and need to ask for help. I want to redirect all urls which follow a certain pattern (in this case it contains "reviews/category"). These URLs supposed to be redirect to another url which is made up the first one:
http://[product-url]/reviews/category/[category-url]
supposed to be redirect to
http://[product-url].html
Furthermore it shouldn't matter if you call the url with or without www.
Example:
http://example.com/ford-blues/reviews/category/cars supposed to be redirect to http://example.com/ford-blues.html
Any help would be much appreciated.
Following code isn't working
RewriteEngine On
RewriteRule ^reviews/category/?$ $1\.html [R=301,L]
Try:
RedirectMatch 301 ^(.*)/reviews/category/ /$1.html
in the htaccess file in your document root.
Or using mod_rewrite:
RewriteEngine On
RewriteRule ^(.*)/reviews/category/ /$1.html [L,R=301]

htacess url rewrites for .php?=n

I'm looking to rewrite old PHP pages to new locations, however the previous system used URLs structured as follows:
/old-page.php?page=Our%20Services
I've tried the following:
Redirect 301 /old-page.php?page=Our%20Services http://www.newdomain.co.uk/our-services/
Can someone help explain to me why the rewrite rule ignores everything after the question mark please?
You can use Redirect for simple cases only. If you want to check the query string, you must use mod_rewrite and RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} page=Our%20Services
RewriteRule ^old-page.php$ http://www.newdomain.co.uk/our-services/ [R,L]
This checks, if the query string contains page=Our%20Services and the URL path is old-page.php. It then redirects the client to the new URL http://www.newdomain.co.uk/our-services/.

htaccess 301 Redirection based on the query string for urls

I've a live url some thing like this,
http://example.com/today.php?year=2012&date=24&mon=07
and i want it to redirected to
http://example.com/holiday-today/year/mon/date
I tried with %{QUERY_STRING} but i dont know how to get three query parameters and pass them to the redirected url.
How can i do this using htacess?
Try adding this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /today\.php\?year=([0-9]+)&date=([0-9]+)&mon=([0-9]+)
RewriteRule ^today\.php$ /holiday-today/%1/%2/%3? [R=301]
I would reference these similar questions with great answers:
Query string redirection with htaccess
301 redirect from URL with query string to new domain with different query string

Redirect old page url after .htaccess url rewriting

I have rewrited my url's with htaccess and now I want to redirect the old url's to the new ones, but I can't figure it out how to do it after all.
This is my redirect rule used:
RewriteRule ^page/([^/]*)/$ /page.php?name=$1 [L]
The old url's look like this: page.php?name=page-name
The new url's look like this: /page/page-name/
That's a bit complex when you want redirect an url with GET parameters.
Here's a trick to do it :
RewriteRule ^page\.php$ %{QUERY_STRING} [C]
RewriteRule name=(.*) /page/$1/? [R=301,L]
Explainations :
First, you redirect page.php?name=page-name to ?name=page-name
Then, you ask using the following rule for this result (with [C] tag)
Third, you redirect page-name, picked with (.*) to page/page-name/
Last trick, if you don't put the last ?, your query string will be appended to your result and you'll have this kind of url : page/page-name/?name=page-name. Using an useless ? erase the old GET parameters.
Found some informations here :
Apache mod_rewrite doc
How to strip a query string

301 redirect from URL with query string to new domain with different query string

I am trying to figure out how to do a 301 redirect in my htaccess file to redirect some files to a new domain. Here's what I need to know how to do:
OLD URL: http://www.example.com/index.php?page=news&id=2366
NEW URL: http://www.example2.com/news.php?name=23546
The redirects don't have to be automatically created. I can hard-code the pages I need to redirect in the htaccess file, I just don't know the format to use as I've read that a "standard" 301 redirect won't work with query strings.
Basically this is what I want to do, but from my research so far it doesn't sound like it can be done this way.
redirect 301 /index.php?page=news&id=2366 http://www.example2.com/news.php?name=23546
You could use a rewrite rule with a query string match condition, such as:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^page=news&id=2366$
RewriteRule ^(.*)$ http://www.example2.com/news.php?name=23546 [R=301,L]
Checkout this blog page for more information on how this works.
I had the same problem, but still more complicated, because I needed to discard other parameters.
Like this: my-old-page.php?a=1&b=2&c=3
I need to use one of the strings and discard the others, but that solution only worked if I want to use the last parameter (c=3). If I want to use any other (a=1 or b=2) it runs to a 404. After much struggling and searching, I found an answer:
RewriteCond %{QUERY_STRING} ^.* ?b=2.* ?$ (without space after the *)
RewriteRule (.*) http://www.my-webpage.php/new-location-2? [R=301,L]
The solution is to add ".*?" before and after the parameter to use.
I don't know if this is the best solution, but it works.

Resources