I have many subdomains within my main example.com like us.example.com, gb.example.com, fr.example.com, pl.example.com etc.
Now I need to do the 301 redirects from the non-existing sites to the new ones. Normally I would have done it the simplest way possible:
Redirect 301 /system_example/systems/system_abc.html http://pl.example.com/system_example/systems/all_systems.html
But this is not possible in the website structure that I have as all the other subdomains that have that path /system_example/systems/system_abc.html will be now redirecting to the http://pl.example.com/system_example/systems/all_systems.html
I only want to do the redirects within the pl.example.com subdomain - all the others need to be left untouched. If only this type of redirect worked but it does not:
Redirect 301 http://pl.example.com/system_example/systems/system_abc.html http://pl.example.com/system_example/systems/all_systems.html
Is it at all possible do achieve that via .htaccess?
If you have mod-rewrite enabled, then:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^pl\.example\.com$ [NC]
RewriteRule ^/?(system_example/systems)/system_abc\.html$ /$1/all_systems.html [R=301,L]
otherwise, using mod-alias only:
<If "%{HTTP_HOST}" == "pl.example.com">
RedirectPermanent /system_example/systems/system_abc.html /system_example/systems/all_systems.html
</If>
I am not sure whether the second one is syntactically correct or not.
Related
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.
I have an old site with urls A and B
A. www.site.com/about
B. www.site.com/about/subpage
I need to redirect
A. www.site.com/about-us
B. www.site.com/not-a-sub-anymore
My htaccess contains
Redirect 301 /about /about-us
Redirect 301 /about/subpage /not-a-sub-anymore
The first redirect works fine, however, my second redirect seems to inherit the first rule resulting in redirecting me to www.site.com/about-us/not-a-sub-anymore which results in a 404 obviously. How can I work around this issue?
edit: I am using apache2.2.3 and I see in apache 2.4.8 they've added a RewriteRule IgnoreInherit which seems like something I'd be looking for. I have no control of my version of apache unfortunately.
I would suggest redirecting via RewriteRule to prevent additional rules from firing:
RewriteRule ^about/subpage$ /not-a-sub-anymore [R=301,L]
RewriteRule ^about$ /about-us [R=301,L]
However, if you insist on using Redirect 301, I'd try rearranging the order:
Redirect 301 /about/subpage /not-a-sub-anymore
Redirect 301 /about /about-us
I have a website and need everything in my subdomain to redirect to my home page.
For example I would need www.website.com/sub to redirect back to www.website.com
same with anything else within /sub
so www.website.com/sub/sdjdj/sdsd/dsd ....would also need to redirect to www.website.com
How can I achieve this with htaccess?
You can use:
RewriteEngine on
RewriteRule ^sub(?:/|$) / [NC,R,L]
You can use this in your .htaccess in the root.
RewriteEngine On
RewriteRule ^sub(?:$|/.*) / [R=302,L]
When you are sure the rule is working as intended, change to 301 in rule.
I am having trouble redirecting an entire directory that no longer exists on our server.
All variations of the following are not working and I simply get a 404 Page Not Found.
.htaccess file looks like this:
redirect 301 /non_existent_directory/ http://my.website.com/existent_directory/
Is it possible to use the Redirect 301 directive for this? Or is this only solvable by mod_rewrite?
Thanks
I even tried:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com\/non_existent_directory\/$ [NC]
RewriteRule ^(.*)$ http://my.website.com/existent_directory/ [R=301,L]
No luck...
From the Redirect documentation, I would say
Redirect 301 /non_existent_directory http://my.website.com/existent_directory
or
Redirect 301 /non_existent_directory /existent_directory
should work, provided you are allowed to use this in a .htaccess file. See also Troubleshooting .htaccess files. You should test without 301 though, to prevent caching of bad redirects by the client.
If this doesn't work, you can try a RewriteRule of course
RewriteEngine On
RewriteRule ^/?non_existent_directory(.*)$ /existent_directory$1 [R,L]
But this is equivalent to the above Redirect directive.
IS there a way with just htaccess 301 redirect to redirect any page on a domain to a specific page on another domain.
eg. I was domain.com/index.html and domain.com/contact.html to both redirect to newsite.com/index.html
But I am wanting to do this without having to list each of the pages specifically.
can my 301 redirect be just something like
301 * http://newsite.com/index.html
or how should it be set up. Unfortunately I don't have access to mod rewrite so I cant use mod rewrite to make it work.
Had an issue similar to this using wordpress and trying to remove all .asp extensions from pages, this worked pasted at the top of my .htaccess file
## 301 Redirects
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]
Yes, that is possible -- instead of mod_rewrite you need to use mod_alias (which has more chances to be enabled).
This one will redirect everything to index.html on newsite.com/
RedirectMatch 301 ^/(.*)$ http://newsite.com/index.html
This one will redirect everything to the same path but on another domain: (e.g. oldsite.com/meow.php => newsite.com/meow.php)
RedirectMatch 301 ^/(.*)$ http://newsite.com/$1