need redirection 301 .htaccess - .htaccess

What Code do i use on .htaccess file to redirect 301 these domains:
https://example.com/* -> https://www.example.com/* -> https://ww.example.com/* -> https://wvw.example.com/* = to https://ww1.example.com/*
thanks in advance for any help.

Well, you need a redirection rule and one condition per host name:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^ww\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ https://ww1.example.com%{REQUEST_URI} [QSA,R=301]
If you actually want to redirect all requested hosts your http server responds to over to "ww1.example.com" then this can be simplified:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^ww1\.example\.com$
RewriteRule ^ https://ww1.example.com%{REQUEST_URI} [QSA,R=301]
Here is the documentation, it helps a lot to actually read the documentation of the tool you use:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
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).

Related

Redirect to HTTPS and WWW in one single redirect

For my website, I have setup CloudFlare CDN. The website is hosted on Siteground. CloudFlare requires the websites to be redirected to www.
Now I have setup redirects in the htaccess file. It redirects all non-https and non-www requests to https-www versions.
Following are the rules I have used on the htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
The redirect works fine. My only problem here is that it requires 2 redirects. Let's say my website is example.com.
When I open example.com, it first redirects to https://example.com and then it gets redirected to https://www.example.com
So, I get the response code after two redirects. 301-301-200
Now my question is: Is there any way to have it done in a single redirect?
Do you need a dynamic host name? If not (sounds like), you can simplify this: if not www or not https then redirect to https/www. If the host name is static you do not need to grab it from a condition. You can name it inside the redirection rule literally:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com{REQUEST_URI} [QSA,R=301,END]
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 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).

how to rewrite url index.html to /home in htaccess

i want to rewrite url with index.html to /home, right now my url is like this https://xxxx.com/index.html, i want to convert it as https://xxxx.com/home , can anyone please help me how to do this in htaccess ? here i did some work for it but it is not working can anyone please help me how to do it ? any help will be really appreciated, this is what i tried with the code
CheckSpelling Off
AddType x-httpd-php56 .php
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index\.html$ /home [NC,R,L]
Nearly perfect, but you need to add the internal rewriting the other way 'round... The incoming request is what you want to rewrite, so a request to /home...:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,END]
RewriteRule ^/?index\.html$ /home [R=301,END]
RewriteRule ^/?home/?$ /index.html [END]
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 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).

Mod-rewrite rule which just gets the part of the URL after the =

I'm currently trying to redirect this URL
http://dev.example.org/active/researchers/contact.php?IDENT=12345
to
http://portaldev.example.org/users/ident/12345
in htaccess.
However, I can only get a redirect to
http://portaldev.example.org/users/ident/IDENT=12345
because I can't find a way to get rid of the IDENT=. How can I do that?
The rewrite conditions in my htaccess are:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=([0-9]*)$
RewriteRule ^(.*)$ http://portaldev.example.org/users/ident/$2 [R=302,NC,L]
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)IDENT=(\d+)(?:&|$)
RewriteRule ^/?active/researchers/contact\.php$ http://portaldev.example.org/users/ident/%1 [R=302,QSD,NC]
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).
Got the answer to my question for anyone else who needs help:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=(.*)
RewriteRule (.*) http://portaldev.cepr.org/users/ident/%1? [R=301,L]
I think the main thing was the ? at the end of the rewrite rule but it also wouldn't work unless I put in the first RewriteCond to request uri and used (.*) in the rewrite rule

.htaccess Rewrite rule not working on Safari

I am trying to redirect a URL xyz.de to www.xyz.de. For that I am utilizing .htaccess but it doesn't work in Safari and Firefox.
Here's what I am doing.
RewriteCond %{http_host} ^xyz.de [nc]
RewriteRule ^(.*)$ http://www.xyz.de/$1 [r=301,nc]
This should do what you are looking for:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=301]
Note that %{http_host} and %{HTTP_HOST} are something different...
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 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).

How can I Redirect subdomain with path to root domain without this path

I need help with this case
I need to redirect (301) a subdomain test.example.com/?preview_theme=niss-promag
to https://example.com without /?preview_theme=niss-promag
I already used code but it's redirecting to root domain with the same path https://example.com/?preview_theme=niss-promag
This is the code currently used:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ https://example.com [L,R=301]
Note: I need to redirect whatever path of the subdomain to root domain I don't specify the one above!
This should be working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com [QSD,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...
If that does not work then most likely your http server is outdated, try something like that:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{QUERY_STRING} ^preview_theme=niss-promag$
RewriteRule ^(.*)$ https://example.com/? [R=301]
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).

Resources