htacess url rewrites for .php?=n - .htaccess

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/.

Related

Rewrite url to another domain while maintaining the last part of url

I have a question similar to this
URL rewrite for part of domain name
Tried to use that rule but it didn't work. I think the url rule could be different.
I have the url:
olddomain.com/test/ref/s/page
olddomain.com/test/ref/s/page1
olddomain.com/test/ref/s/page2
I need to rewrite them and maintain the last part of the url, eg:
https://www.newdomain.com/test/ref/s/page
https://www.newdomain.com/test/ref/s/page1
https://www.newdomain.com/test/ref/s/page2
Edit:
The ending of the url is dynamic so using individual redirection for each link is not practical.
Much thanks for any help.
You can use your .htaccess file to do this, similar to the answer you are referencing. If you just want to send your old urls to your new urls individually, you can achieve this quite easily using the Redirect 301 function inside .htaccess, and uploading to the root of "old domain", like so:
Redirect 301 /test/ref/s/page https://www.newdomain.com/test/ref/s/page
Redirect 301 /test/ref/s/page1 https://www.newdomain.com/test/ref/s/page1
Redirect 301 /test/ref/s/page2 https://www.newdomain.com/test/ref/s/page2
This will individually 301 the index at each of the old urls to the corresponding new url.
If you want to redirect everything from "old domain" to "new domain" just use a rewrite like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
You would, of course, need to configure this depending on your use of SSL.

htaccess redirect from page with specific query string

I'm new to the rewrite rules in htaccess and need some help with the following:
Page I want to redirect from:
http://example.com/webinar/?confirmed
Page I want to redirect to when a user tries to access the above URL:
http://example.com/webinar-thanks
What makes this tricky is that I will still need to access the original URL with different query strings such as:
webinar/?stats
webinar/?console
And so on. I tried to do a 301 redirect but that redirects any attempt to access the /webinar page, regardless or the query string at the end, so I guess I need some rewrite syntax?
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^confirmed$
RewriteRule ^webinar/?$ /webinar-thanks [L,R]

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]

Redirect only one URL with parameter

I need to redirect URL, for example:
www.mydomain.com/category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34
To:
www.mydomain.com/category/sub-category/good-product.html
I have a multiple URLs with parameters that need to be redirected to only one or couple of URLs, can you help me, I used Google for hours.
I was try this code at .htaccess:
redirect 301 /category/sub-category/product?page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34 www.mydomain.com/category/sub-category/good-product.html
But it doesn't work.
You can't match against the query string in a Redirect directive. You'll need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=shop.ask&flypage=flypage.tpl&product_id=181&category_id=34$
RewriteRule ^category/sub-category/product$ /category/sub-category/good-product.html? [L,R=301]

What to do with Question Marks in HTAccess Redirects

I'm new to redirects and have researched how to do this but am just getting more and more confused.
What I'm wanting to do is create a rule to redirect:
From: http://www.example.com/wordpress/?p=1250
To: http://www.otherexample.com/blog
Where I'm running into issues is with the question mark. I've read somewhere that I'm not sure I'm doing this correctly, but here's what I have so far:
**Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=1297$
RewriteRule ^wordpress$ linktoothersite [R=301, QSA, L, NC]**
I'm failing somewhere, any of you see what I'm doing wrong? Any help would be appreciated. I could only post two links, so link to other site would be the link.
If it's a single link you want to change you don't need a htaccess rule for that you can just do a redirect.
redirect 301 /wordpress/?p=250 http://www.othersite.com/blog
If the URL parameter changes you can use this rule.
RewriteEngine on
RewriteRule ^wordpress/(.*) http://www.othersite.com/blog [R=301,QSA,NC,L]
Note that when you use the QSA flag it will append the URL with the query parameter so if that's what you want the resulting URL will be.
http://www.othersite.com/blog?p=250
Otherwise if you remove the QSA flag, then it will just redirect to this
http://www.othersite.com/blog

Resources