redirect https://domain to https://www.domain in httpd24 - apache

I'm trying to redirect URL responses from https://domain.com to https://www.domain.com in the apache24 configuration, I'm using Redhat Server.
I have tried to create this Rewrite Condition but its not working
RewriteCond %{HTTP_HOST} ^(domain\.com)?$
RewriteRule (.*) https://www.domain.com/$1 [R=301,L]

I have fixed the Issue by using Directive IF Condition powered by Apache2.4
I have added this code to my ssl.conf file where it redirected all requests sent to https:// domain .com to https:// www .domain .com . Add this code before the Tag :
<If "%{HTTP_HOST} != 'www.domain.com'">
Redirect "/" "https://www.domain.com/"
</If>
Thank you all for your comments and trying to fix my issue, really its something I appreciate it. Please take a look # for more information about httpd24 directives https://httpd.apache.org/docs/2.4/mod/directives.html

() is a capturing group. You don't want that when you check HTTP_HOST unless you're going to use it as part of the new URL. Also make sure to add the ^ (start of string) and $ (end of string) on your RewriteRule.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301,NC]
You can test your htaccess here http://htaccess.mwl.be/ without having to upload and refresh your site.

Related

mod_rewrite and redirect to the same domain with added parameters

I've got a plenty of domains with A records set to the IP of my server.
Apache is processing all this domains with a single virtual host.
What I need is to redirect 'mydomain.com' to 'mydomain.com/?param=mydomain.com' with mod_rewrite, but I don't know how to do this.
I've tried this:
RewriteRule / /?param=%{SERVER_NAME}
with no result. I'd be grateful for any help.
If you really want to redirect example.com/ to example.com/?param=example.com you will need to use the R flag , try :
RewriteRule ^/?$ /?param=%{SERVER_NAME} [L,R]
And if you want to internally redirect example.com/ to example.com/?param=example.com try the following :
RewriteRule ^/?$ /?param=%{SERVER_NAME} [L]
I have solved the problem of eternal redirection by adding RewriteCond %{QUERY_STRING} ^$

apache redirect 301 to .php file extension

for our client project we need to redirect all url calls to the matching .php file extension. this is done for SEO (google) ranking. avoiding indexing both url (with *.php and without)
we try to do this using the .htaccess file (shared host) but it seemed to only work for redirects that have a different url and not to the one that just adding the ".php" extension.
different url works
abc -> abc2.php
same url doesnt work
abc -> abc.php
here is our code sample:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co.uk
RewriteRule (.*) http://www.abc.co.uk/$1 [R=301,L]
# this doesn't work
Redirect 301 /test http://www.abc.co.uk/test.php
# this redirect works as the url is different
Redirect 301 /test-abc http://www.abc.co.uk/abc.php
we host with 1&1 shared host server.
php version is 5.5
I also tried this code that redirects but the page not loading now:
RewriteRule ^(.+)\.(.*)$ http://abc.co.uk/$1.php [R,NC]
also noticed url without file extension (.*|html) will not redirect
Try this .htaccess in DocumentRoot:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.abc\.co\.uk$ [NC]
RewriteRule (.*) http://www.abc.co.uk/$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=302]
This appears to be a 302 re-direct, which is not SEO freindly. I tested the above and it doesnt work, also, if it did I wonder what would happen to url's that did not require the .php extension on the URL.
Basically we're trying to get a 301 re-direct working, but it seems to be conflicting with the rules set up that contain the same url. (IE abc > abc.php)
I wonder if there could be some kind of Apache setting or even and unlikely a PHP setting? the 301's work on any other linux server that I try.

301 redirect for all get request

I need to redirect all incoming requests get. for example: site.com?anystr to site.com
I tried to do so
RedirectMatch /?(.*)$ site.com
But it causes cyclic forwarding and i get browser error
RedirectMatch does match anything behind the domain name and before the query string. The regex /?(.*)$ will match any request.
You will need to use mod_rewrite. Enable mod_rewrite in the main config file of Apache and restart Apache. Then add the following to your .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ - [QSD,R,L]
Change the [R] flag to [R=301] after testing this works as expected.
I believe this is hat you want:
RewriteEngine on
RewriteCond %{QUERY_STRING} .+
RewriteRule ^ /? [L,R=302]
i.e. any URL with query string is redirected to root with query string stripped off using ? in the target.

redirect based on directory in path via .htaccess

Via .htaccess, how do you redirect this
http://www.somedomain.com/de/foo
to this:
http://www.de-domain.com/foo
The redirection should depend on the second parameter, in the example above "de".
Depending on the server configuration you should be able to do this on the .htaccess for somedomain.com. If this works appropriately you might try [R=301,L] to make the redirection permanent:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?somedomain.com$
RewriteRule ^([a-z]{2})/(.*) http://www.$1-domain.com/$2 [R,L]

Redirect wildcard subdomains to subdirectory, without changing URL in address bar

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
Here's what I have so far:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...
First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
Now, if you open asd.domain.com it should redirect you to domain.com/asd.
You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:
/
var/
www/
html/
.htaccess
subdomain1.domain.com/
subdomain2.domain.com/
subdomain3.domain.com/
Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.
Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.