I have a domain with two versions and I need to redirect 1 of the versions
test.example.ca
test.example.ca/en
test.example.ca/fr
I need the first domain test.example.com to redirect to test.example.ca/en anytime someone hits it. but i don't want test.example.ca/fr to redirect to test.example.com/en/fr
this is what I've been trying with no success.
RewriteCond %{HTTP_HOST} =test.example.ca
RewriteCond %{HTTP_HOST} !=test.example.ca/fr
RewriteRule ^(.*)$ https://%{HTTP_HOST}/en/$1 [R=301,L]
I understand the question such that you simply to not want requests to https://test.example.com/fr... to get redirected. So you want an exception.
I'd say this roughly is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{REQUEST_URI} !^/fr
RewriteRule ^ https://test.example.ca/en%{REQUEST_URI} [R=301,L]
Chances are that your question was wrong in a few details, to me it reads as if you were not really precise with your host names. But above should be the correct answer to what you actually asked.
You should implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"). That file should be located in the DOCUMENT_ROOT folder defined for your http host. And you need to make sure that the interpretation of such files is enabled at all (see the documentation for the AllowOverride directive for that).
It is a good idea to start out using a R=302 temporary redirection first. And to only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues on the client side.
Related
I try several things for my redirections but without working solution.
I have 2 domains pointing in the same folder/website (mulitshops prestashop) and I would like to redirect some page from the first domain on a specific page and keep domain.
My work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
But I cant do this with simple Redirect 301 because I can't specify the domain from request URI.
Beause the target page it's not the same according to the domain.
Sorry for my english and thank you in advance for your help.
In addition to what #DusanBajic explained in his comment to your question you also need to consider the difference between absolut and relative path in rewriting rules. This is actually explicitly documented...
WHen implemented in distributed configuration files (".htaccess") the rule pattern is matched against the relative path of the requested URL. You however try to match it against an absolute path which will never match. So either change your patterns to use relative paths too, or, preferably, implement your rewriting rules such that they work in both cases. So also when the rules are implemented in the real http server's host configuration where the pattern is matched against the absolute path inside the requested URL. This appears confusing at first. But it does make total sense, once you think about it.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out..
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
My .htaccess file has redirect and rewrite code. It all works fine except for one page. I need to redirect https://example.com/shopping/ceylon-cinnamon-c-62.html and http://example.com/shopping/index.php?cPath=62 to https://example.com/ceylon-cinnamon-c-2_19.html I have tried the four lines under the NONE OF THESE WORK below (one at a time) but the redirect never works. The result url is https://example.com/c-62.html. Can anyone point out the problem or how to test it?
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
# NONE OF THESE WORK
Redirect 301 /shopping/ceylon-cinnamon-c-62.html https://example.com/index.php?cPath=2_19
Redirect 301 /shopping/index.php?cPath=62 https://example.com/index.php?cPath=2_19
RewriteRule ^(.*?)shopping/ceylon-cinnamon-c-62.html$ https://example.com/index.php?cPath=2_19 [R=301,L,NC]
RewriteRule ^(.*?)shopping/index.php?cPath=62$ https://example.com/index.php?cPath=2_19 [R=301,L,NC]
# THESE ALL WORK
RewriteRule ^shopping/(.*)$ /$1 [R=301,NC,L]
#redirect index.php to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://example.com/ [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteRule ^(.*)-c-(.*).html$ index.php?cPath=$2&%{QUERY_STRING}
Your question is a bit vague... The example rules you give ("NONE OF THESE WORK") have little to do with what you ask in your question. And the claimed result of the rewriting attempts certainly is not what your attempts implement. So either your description is incorrect (written from memory maybe?) or you have some other factor in place which you do not tell us about (some application logic maybe that implements its own redirection?)...
Anyway, here are the simple rules to implement the exact redirection you ask about, independent of any other stuff:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)cPath=62(?:&|$)
RewriteRule ^/?shopping/index\.php$ /ceylon-cinnamon-c-2_19.html [R=301]
RewriteRule ^/?shopping/ceylon-cinnamon-c-62\.html$ /ceylon-cinnamon-c-2_19.html [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
That said: when testing make tripple sure that you are not looking at cached results. Always use a fresh anonymouse browser window when testing, make deep reloads , not just reloads and watch your browser networking console for the actual response you receive...
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I want to mask a folder in URL, so instead of www.mysite.com /employers/university-of-worcester/profile.html there would be www.mysite.com /university-of-worcester/profile.html , meaning that sub-directory "employers" is hidden.
There will be many folders created inside "employers" folder.
This is a matter of modifying .htaccess, I have tried a lot of solutions that I have found on stack.
The latest line of code I tried to add is:
RewriteRule ^employers/(.*)$ /$1 [L]
My .htaccess looks like this now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^employers/(.*)$ /$1 [L]
</IfModule>
If I go to URL www.mysite.com/employers/ this will redirect to www.mysite.com/ (the homepage), which I am happy about, but if I try www.mysite.com/university-of-worcester/profile.html I get "The page can’t be found.", but the home profile.html is definitly inside "university-of-worcester" folder.
I am using wordpress.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/employers/?(.*)$ /$1 [R=301,QSA]
RewriteCond %{REQUEST_URI} !^/employers/
RewriteRule ^/?(.*)$ /employers/$1 [END,QSA]
This will redirect direct requests to that folder and internally rewrite requests to that folder.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I have a Domain which I want to redirect to New Domain using 301 redirection on htaccess; I did that successfully but now the problem is, I want to redirect pages / folders of old domain to various locations of new domain. E.g.
www.olddomain.com redirect to www.newdomain.com (works)
www.olddomain.com/about should redirect to www.newdomain.com/who-we-are (doesn't work)
I tried redirect 301 oldurl newurl on my htaccess but it won't work as root already redirects to new domain.
is there any way to solve this issue?
Certainly what you ask is possible. Most likely you had an issue with the order of your rules if things did not work for you. You need to keep in mind that rules are processed from top to bottom. So more specialized, more detailed rules have to come first, more general rules further down.
Here is an example for the single example you gave:
RewriteEngine on
# this is a specialized rule for a specific resource
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/about/?$ https://newdomain.com/who-we-are [R=301]
# this acts as a fallback for a request not yet redirected
RewriteCond %{HTTP_HOST} ^olddomain\.com$
RewriteRule ^/?$ https://newdomain.com/ [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
You can use this :
RewriteEngine on
#redirect specific pages to specific location on new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?about/?$ http://newDomain.com/who-we-are [NC,L,R=301]
#redirect the old domain root to new domain
RewriteCond %{HTTP_HOST} ^(www\.)?oldDomain\.com$ [NC]
RewriteRule ^/?$ http://newDomain.com/$1 [NC,L,R=301]
You can remove the %{HTTP_HOST} condition in both rules above if your domains (old and new) are on different servers or pointing to a different document root on the same server.
Thanks, everyone, issue is resolved.
RewriteEngine on
RewriteCond %{HTTP_HOST} hitechos\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ https://www.hitechdigital.com/ [L,R=301]
Regards
Manoj Soni
Trying to redirect url to specific url which includes /for-your-practice/.
if we get to know that url contains for-your-practice just redirect it to the url i want. which would be https://www.example.com/first/second/order/shop/product-category/for-your-practice/
Tried solutions so far:
First:
Redirect /for-your-practice/
https://www.example.com/first/second/order/shop/product-category/for-your-practice/
Second:
Redirect 302 https://www.example.com/first/second/order/shop/for-your-practice/ https://www.example.com/first/second/order/shop/product-category/for-your-practice/
Third:
#RewriteCond %{REQUEST_URI} www.example.com/first/second/order/shop/product-category/for-your-practice [NC]
#RewriteRule ^(.*)$ /for-your-practice [R,L]
Fourth:
RewriteCond %{QUERY_STRING} /for-your-practice/
RewriteRule .*$ http://www.example.com/first/second/order/shop/product-category/for-your-practice [L,R=301]
unfortunately none of them worked, what's wrong i'm doing here ?
.htaccess
Easiest probably is to use apache's rewriting module:
RewriteEngine on
RewriteCond %{REQUEST_URI} /for-your-practice/
RewriteRule ^ https://www.example.com/first/second/order/shop/product-category/for-your-practice/ [R=301]
For this some preconditions have to apply:
the interpretation of .htaccess tyle files has to be enabled
the rewriting module has to be loaded
the .htaccess style file has to be located in your DocumentRoot
the .htaccess style file has to be readable by the http server process
Reason for the use of an additional RewriteCond is that preceding slashes in URLs can raise issues with RewriteRules in .htaccess style files. IN general you should always prefer to place such rules in the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often without need. Those files are only provided as a last means for situations where one can not use the host configuration (read: really cheap hosting providers) or for applications that need to write their own rewriting rules (which obviously is a security nightmare).