Rewrite url to another domain while maintaining the last part of url - .htaccess

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.

Related

Changing my website from dynamic CMS to static HTML. Should I 301 redirect or use other methods?

I am changing my website from a dynamic CMS-system (Umbraco) to a static classic .HTML. It is the on the same domain, but the URL will change.
Example: The URL is changing from:
www.example.com/information
To:
www.example.com/info.html
My question is:
What is the best way to redirect while keeping the best SEO page rank.
I am thinking about 301 redirect through .htaccess, but I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
Or maybe there is a different better way?
I do have a fine 404.
Also I need the right redirect code for .htaccess - if that's the right way.
I hope you guys can help me out.
I haven't try anything out yet, because I don't wanna do 301 before the site go live.
You need to implement 301 redirects from the old URL to the new URL in order to preserve SEO and ensure that any "old" links that have been bookmarked or linked to from other websites still work.
Exactly how you implement the 301 redirect (either in your server-side script or in .htaccess) does not really matter. However, if you are moving to an entirely static site then .htaccess is likely the only option you have.
I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
You need to redirect from the "old" URLs to the "new" URLs that you are using/linking to on the new site. (It makes no sense to redirect the other way as that would just break everything!)
You can probably just use the simple mod_alias Redirect directive.
For example, to 301 redirect from /information to /info.html you could do the following:
Redirect 301 /information /info.html
Bear in mind that 301 redirects are cached persistently by the browser. To prevent caching issues it is advisable to test first with a 302 (temporary) redirect.
Have you considered keeping the same URLs? This would obviously negate the need for implementing redirects. You could employ URL-rewriting if the underlying file is called info.html. For example, using mod_rewrite:
RewriteEngine On
RewriteRule ^information$ info.html [L]
The above would internally rewrite a request for /information to info.html. The user only sees /information in the browser address bar, but info.html is served from your site.
Taking this further, it would be easier if the new "file" is simply the same as the old URL, just with a .html extension. For example, the URL is /information and the underlying file is information.html. You can then use a single rule to rewrite all your URLs. For example:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
The above assumes the old URLs do not contain additional slashes (ie. consist of a single path segment. In other words, all files are in the document root) and do not contain dots.
White
I finally got my page ready to go live, and i changed all my new URLS to the same name as the old URL, just with a .html extension - as u said. After that i used:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
And it works fine.
I do have a question about if a "RewriteRule ^(.*).html$ /$1 [L,R=301]" would be better? I mean both "/page.html" and "/page/" works, and this could mess with my former SEO ranking?
Also: what do u think of this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And by the way. Do you use "page.html" or just "/page" in your href to prevent the .html showing?
//MM

How to redirect multiple old urls to new urls using a 301 redirect via htaccess

I have recently change some URLs from my website to make it more SEO friendly. I have old urls that I would like to link to new urls. The old urls are no longer available but I would like the old urls to redirect to the new urls.
My current 301 redirect code below is for http:// to https://www, which I need.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301,NC]
How do I make it so that I can also redirect from old urls to new urls as well. Is it possible with the 301 redirect?
Old url examples:
www.mywebsite.com/book-stories.html
www.mywebsite.com/book-journals.html
New url examples:
www.mywebsite.com/stories.html
www.mywebsite.com/journals.html
Any help would be much appreciated.
If you only have a handful, static redirections you can do it appending RewriteRules just as you are doing:
RewriteRule ^book-stories.html$ stories.html [R=301]
RewriteRule ^book-journals.html$ journals.html [R=301]
If they all have a fixed pattern, you could exploit that to reduce the amount of rules.
RewriteRule ^book-([a-z0-9.-]+).html$ $1.html [R=301]
If you have many different rules that you will be updating, you can look into indexed maps or even database-based RewriteMaps
I tried the above code and didn't work and to be honest I could have been doing it incorrectly. I did look into it further and tried another 301 redirect
Redirect 301 book-stories.html https://www.mywebsite.com/stories.html
Redirect 301 book-journals.html https://www.mywebsite.com/journals.html
That worked for me. Hope this helps anyone else who will encounter this issue.

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

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources