I've got an existing Rewrite rule that adds a prefix of /us for US users and redirects them to different pages that contain the /us prefix (so 'page-1' is redirected to 'us/page-1'):
RewriteRule ^/(page-1|page-2|page-3|page-4)$ /us/$1 [redirect=temp,last]
I'm just needing the opposite of this rule for the home country of the site, so it strips the /us prefix for these 4 pages if someone from the home country navigates to these pages (so 'us/page-1' should become 'page-1').
I'm not needing the conditions, just the rule.
Only capture the part you care about.
RewriteRule ^/us/(page-\d)$ /$1
Related
I am moving my WP ecommerce site to a new domain and I need to code a more advanced htaccess 301 redirect to pass on the SEO love. (I say htaccess but maybe there is a server side way that is better)
I have made sure, as much as possible, to keep the URL structure the same so for the products/posts, categories, tags and most pages everything after the olddomain.com/XXXXX is the same.
However, I don't want to do a blanket redirect for everything because there are some parts of the site that will not match so I thought it better to break into into chunks/functions.
(Maybe this is a bad strategy and I should just do do one blanket redirect and the trouble shoot page not found as it all goes live?)
redirect function for products
redirect function for categories
redirect function for tags
individual redirects for the rest
There are also three languages with sub folder /ca/ and /es/ - example.com/es/products - assuming I can just copy the function for each language and appending the subfolder.
Examples:
oldomain.com/product/any-product-ABC
redirect to
newdomain.com/product/any-product-ABC
(Domain change) (folder same) (product added from previous)
Then same redirect for languages
oldomain.com/es/product/any-product-ABC
redirect to
newomain.com/es/product/any-product-ABC
How do I write the above redirects?
I say htaccess but maybe there is a server side way that is better
If you have access to the Apache server config then you can indeed simplify these redirects, which will also be more efficient since it will reduce the load (if any) from the main site.
Create a separate <VirtualHost> container for the old domain and then you can use simpler mod_alias directives.
For example, for the two examples you gave. eg. /product/<product-name> or /<lang-code>/product/<product-name> to the same URL at the new domain then you can use following single rule:
RedirectMatch 301 ^(/[a-z]{2})?/product/[\w-]+$ https://newdomain.com/$0
This matches any optional 2-character language code. If you only have three languages then you could be more specific and use regex alternation instead, eg. (ca|es|id) in place of [a-z]{2}. The <product-name> is assumed to consist of the following characters only: a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
However, if you are restricted to .htaccess and both domains resolve to the same place then you will need to use mod_rewrite and check the requested hostname. For example, the following would need to go near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^((ca|es|id)/)?product/[\w-]+$ https://newdomain.com/$0 [R=301,L]
Note the regex is slightly different to the mod_alias RedirectMatch directive used above since the RewriteRule pattern (first argument) matches against the URL-path less the slash prefix.
Do not repeat the RewriteEngine directive if it already occurs elsewhere in the config file. The order of the directives is important. The above redirect must go before any existing internal rewrites and ideally before any canonical redirects (to minimise the number of redirects).
You should first test with 302 (temporary) redirects to avoid potential caching issues.
so I thought it better to break into into chunks/functions.
That's fair enough. Although you need to make sure that any URLs that you don't redirect from the old domain return a 404 or 410.
And it may be possible to combine "products", "categories" and "tags" into a single rule, depending on exactly the format of these URLs.
I am looking to add multiple language support to my website. Is it possible to use the .htaccess file to change something like:
example.com/dir/?lang=en to example.com/en/dir/
example.com/main/?lang=de to example.com/de/main/
example.com/main/page.php?lang=de to example.com/de/main/page.php
Where this works with any possible directories - so if for instance later on I made a new directory, I wouldn't need to add to this. In the above example, I want the latter to be what the user types in/is in the address bar, and the start to be how it is used internally.
Is it possible to use the .htaccess file to change something like:
example.com/dir/?lang=en to example.com/en/dir/
Yes, except that you don't change the URL to example.com/en/dir/ in .htaccess. You change the URL to example.com/en/dir/ in your internal links in your application, before you change anything in .htaccess. This is the canonical URL and is "what the user types in/is in the address bar" - as you say.
You then use .htaccess to internally rewrite the request from example.com/en/dir/, back into the URL your application understands, ie. example.com/dir/?lang=en (or rather example.com/dir/index.php?lang=en - see below). This is entirely hidden from the user. The user only ever sees example.com/en/dir/ - even when they look at the HTML source.
So, we need to rewrite /<lang>/<url-path>/ to /<url-path>/?lang=<lang>. Where <lang> is assumed to be a 2 character lowercase language code. If you are offering only a small selection of languages then this should be explicitly stated to avoid conflicts. We can also handle any additional query string on the original request (if this is requried). eg. /<lang>/<url-path>/?<query-string> to /<url-path>/?lang=<lang>&<query-string>.
A slight complication here is that a URL of the form /dir/?lang=en is not strictly a valid endpoint and requires further rewriting. I expect you are relying on mod_dir to issue an internal subrequest for the DirectoryIndex, eg. index.php? So, really, this should be rewritten directly to /dir/index.php?lang=en - or whatever the DirectoryIndex document is defined as.
For example, in your root .htaccess file:
RewriteEngine On
# Rewrite "/<lang>/<directory>/" to `/<directory>/index.php?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2/index.php -f
RewriteRule ^([a-z]{2})/(.*?)/?$ $2/index.php?lang=$1 [L]
# Rewrite "/<lang>/<directory>/<file>" to `/<directory>/<file>?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([a-z]{2})/(.+) $2?lang=$1 [L]
If you have just two languages (as in your example), or a small subset of known languages then change the ([a-z]{2}) subpattern to use alternation and explicitly identify each language code, eg. (en|de|ab|cd).
This does assume you don't have physical directories in the document root that consist of 2 lowercase letters (or match the specific language codes).
Only URLs where the destination directory (that contains index.php) or file exists are rewritten.
This will also rewrite requests for the document root (not explicitly stated in your examples). eg. example.com/en/ (trailing slash required here) is rewritten to /index.php?lang=en.
The regex could be made slightly more efficient if requests for directories always contain a trailing slash. In the above I've assumed the trailing slash is optional, although this does potentially create a duplicate content issue unless you resolve this in some other way (eg. rel="canonical" link element). So, in the code above, both example.com/en/dir/ (trailing slash) and example.com/en/dir (no trailing slash) are both accessible and both return the same resource, ie. /dir/index.php?lang=en.
I hope I am asking this questions correctly, if I am not, please feel free to correct me.
I am trying to redirect 301 via htaccess file for all user profiles within the same site so for example. The first section shows the actual URLs I want to redirect from and to where, but this is only focused on 1 user account.
Redirect 301 /otsn/members/admin/my-orders/ /otsn/members/admin/shop/
I am thinking can I use the percentage symbol to make this redirect universal to all users, maybe I'm wrong, Can I do the following?
Redirect 301 /otsn/members/%/my-orders/ /otsn/members/%/shop/
I created a better user profile shop tab that has more capabilities, so I want to get rid of the old one and in case anyone tries to enter the old version of that profile page tab by entering the url manually, I want them sent to the new version for their profile shop page but I want this to happen with every user profile in the system.
Is this the correct method? or is there a better more efficient way of doing this?
Thank you
You may use this rule as topmost rule in your .htaccess or Apache config:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([\w-]+)/members/([\w-]+)/my-orders/?$ [NC]
RewriteRule ^ /%1/members/%2/shop [L,R=301]
%1 is back-reference for the first value we are capturing in RewriteCond i.e. first pattern in (...) and same way %2 will represent second captured value in (...).
I am trying to create a .htaccess rule to redirect users from one domain to the other, but this should happen only if the original URL does not exist. For example:
www.domain.com/100 to www.otherdomain.com/PersonalPages/100
www.domain.com/150 to www.otherdomain.com/PersonalPages/150
Now, I don't have a page/file named 100 or 150, so that's why it should redirect, but if user enters "www.domain.com" I do have "index.php" and should stay in "www.domain.com" and not try to go to "www.otherdomain.com/PersonalPages/index.php" (or any other if page exist in my www.domain.com)
I know I can create a simple 301 redirect, but the numbers in the URL are the IDs of users and that number will grow on a daily basis so I can't be adding lines of redirects to my .htaccess for each new user.
Is there a 404 code I could use?
I think this will work for you:
RewriteRule ^([0-9]+)$ http://www.otherdomain.com/PersonalPages/$1
^ and $ mark the beginning/end of the url, [0-9]+ stands for one or more numbers between 0 and 9.
I have a client domain with thousands of pages that are moving to a new domain. The naming convention of the .html has changed, and I know htaccess can handle this somehow.
Here's an example:
old site:
http://oldsite.com/state/cityname-index.html
new site:
http://newsite.com/state/computer-support-cityname-index.html
This is beyond my understanding at the moment. I'd appreciate a little help. Thanks!
If you want to redirect the old website pages to the new one, you can use rewriting like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com$ [NC]
RewriteRule ^([^/]+)/(.+)-index\.html$ http://newsite.com/$1/computer-support-$2-index.html [QSA,R=301]
The first line activates the rewriting in the htaccess.
the second one checks if the current request is for the old website.
if yes, the third line is activated, wich contains a regular expression and a new link to redirect to if that regular expression if matched, the [QSA,R=301] at the end are flags that will affect the rewrite rule.
This rule I wrote to you captures the state (first parentheses) and the cityname (second parentheses) and then it redirects to the new website replacing the $n (where n is the number of the parenthese from before) with the captured content.
The QSA flag (Query String Append) will add any parameter from the old site request to the new generated request.
The R=301 flag will generate a browser Redirection with code 301 (permanant redirect).
For more information about mod_rewrite see http://httpd.apache.org/docs/current/mod/mod_rewrite.html