Looking for tips on writing an htaccess file for a .us to .com site transfer
Example:
https://www.example.us/ to https://www.example.com/
You can do that by using the following in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.us [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.us [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
This will redirect anything from example.us to exmaple.com. It will do this using a 301 redirect.
This includes all directories.
Make sure you clear your cache before you test this.
Related
I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.
This is the code in my.htaccess file
#force non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
This code redirects www.example.com to example.com
But whenever there is something ahead of the url
Example -
www.example.com/file-consumer-something
It redirects me to example.com
I want it to become example.com/file-consumer-something
Can someone help ?
The code you posted already looks like it should work as required. However, it's possibly conflicting with existing (mod_alias / mod_rewrite) directives in your .htaccess file. It's also doing more than you seem to require, so can be simplified.
Try the following instead. This should go near the top of your .htaccess file.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
I have written a simple redirect condition as:
RewriteCond %{HTTP_HOST} !^my-domain\.com$
RewriteRule ^(.*)$ http://my-domain.com/hu/$1 [L,R=301]
It redirects correctly from www.mysite.com to mysite.com/hu/
But it does not redirect mysite.com to mysite.com/hu/
Please help
You've cleary copied this code without understanding it. This is a typical htaccess to remove the www. part of a domain.
To redirect your homepage to a subfolder, use this code instead :
RewriteCond %{HTTP_HOST} ^(www\.)?my-domain\.com$
RewriteCond %{REQUEST_URI} !^hu/$
RewriteRule ^(.*)$ http://my-domain.com/hu/$1 [L,R=301]
I have been using .htaccess for few redirects, I am having a hard time finding any solution for a new redirect I want.
I have users on my site and urls look like:
http://www.domain.com/users.php?user=username
Is it possible to rewrite it using htaccess so that it appears:
http://username.domain.com
Any help is much appreciated.
Something along these lines:
RewriteRule ^users.php?user=(.*)$ http://$1.domain.com/ [R,L]
As long as you've set it up so that all subdomains of domain.com point to the same document root as domain.com then you can do this with a single htaccess file in your document root:
RewriteEngine On
# externally redirect to user's subdomain when a specific request is made to users.php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /users\.php\?user=([^&\ ]+)
RewriteRule ^ http://%2.domain.com/ [L,R=301]
# internally rewrite the user's subdomain to users.php
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^/?$ /users\.php?user=%1 [L,QSA]