improvement of a mod_rewrite rule - regex

Could you have a look at the code below and say if this is the correct way to do it please.
In order to redirect from:
http://blog.exampledomain.com/2013/10/testpage.html
to
https: //exampledomain.com/blog/testpage/
I have placed the below rewrite rules in 2 .htaccess files:
In the .htaccess file in root of exampledomain.com I have placed:
RewriteCond %{HTTP_HOST} ^blog\.exampledomain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.exampledomain\.com$ [NC]
RewriteRule ^(.*)$ https://exampledomain.com/blog/$1 [R=301,L]
In the .htaccess file in the blog folder in exampledomain.com/blog I have placed:
RewriteRule ^([0-9]{4})/([0-9]{2})/(.*)\.html https://exampledomain.com/blog/$3/ [R=301,L]
Questions:
Is this the correct way to do it?
Does this mean in this setup we have 2 redirects? (which is probably not good for SEO) Should these be combined? How?

These two redirects should be combined into one.
In the .htaccess file in the document root of your subdomain (which looks it might be the same as the document root of the main domain? Or in the main domains .htaccess file - if this is a parent directory on the filesystem), try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.exampledomain\.com$
RewriteRule ^\d{4}/\d\d/(.+)\.html$ https://exampledomain.com/blog/$1/ [R,L]
When you are sure this is working OK then change the temporary redirect (R) into a permanent one (R=301).
UPDATE: To also redirect from: http://blog.exampledomain.com/ to https://exampledomain.com/blog/ and http://blog.exampledomain.com/whatever to https://exampledomain.com/blog/whatever, add the following directives after the above:
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.exampledomain\.com$
RewriteRule ^(.*)$ https://exampledomain.com/blog/$1 [R,L]

Related

Redirect non-www to www giving me hundreds of 404's

Thanks to anyone who can take a moment to look at this.
Recently I created a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
In this folder I have a htaccess file to which I have added.
## No directory listings
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
When I try to access say http://example.com/subdomain/anytrailingstring then it's NOT redirecting me to http://www.example.com/subdomain/anytrailingstring as I expected, it is redirecting to http://www.example.com/anytrailingstring leaving out the /subdomain/ and this is of course a page that doesnt exist and therefore a 404.
This is a problem.
I do not have any directive in the root .htacces file except for this :
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can someone perhaps see why the subdomain htaccess isnt redirecting to correctly? Did I miss something?
I am not good with htaccess at all, if anybody can help me I would really appreciate it.
Thanks!
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You need to use the REQUEST_URI server variable instead of the backreference ($1). The URL-path matched by the RewriteRule pattern (first argument) is relative to the current directory, so excludes the parent subdirectory (ie. /subdomain in your example).
Do it like this instead:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache since the erroneous (301 - permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
However, a couple of questions:
Why are you not using HTTPS? (You are redirecting to HTTPS in the parent .htaccess file - but this is now being overridden by the mod_rewrite directives in the subdirectory.)
Why not include this in the parent .htaccess file?
UPDATE: So, taking the above points into consideration... if you want to move this rule to the parent .htaccess file in the root then have it like this:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The order of the directives is to ensure there is only ever at most 1 redirect (assuming you are not implementing HSTS).
You were unnecessarily duplicating the RewriteEngine directive (so I removed the second instance).
The RewriteBase directive was not being used.
The capturing subgroup in your HTTP to HTTPS rule was not required. ie. ^ is better than ^(.*)$ in this instance.
Aside:.
...a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
This is a subdirectory, not a "subdomain".
This is a "subdomain":
http://subdomain.example.com/

htaccess redirect files to new domain

I need to direct all files on one domain to the homepage of the new domain (I have to due to a new structure). I seem unable to do this, so for example I use the below code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^olddomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk [R=301,L]
But if I have the below link:
www.olddomain.co.uk/shop/product_info.php?cPath=592&products_id=9335 it does not direct as I want it to, I want it to direct to the homepage of www.newdomain.co.uk but it goes to:
http://www.newdomain.co.uk/?cPath=592&products_id=9335
What do I need to do to amend the htacess on the old domain?
Thanks
You can use this rule:
RewriteCond %{QUERY_STRING} ^cpath= [NC]
RewriteRule ^/?$ /? [R=301,L]
/? in target will strip off original query string.

Can you use htaccess to redirect a subdomain to a file?

I have a bunch of subdomains mapped to my root html folder, but instead of loading the index.php file I want visitors to the subdomains to see a file in that folder named login.php instead.
Visitors to
user-a.mydomain.com
user-b.mydomain.com
etc, should load the file:
mydomain.com/login.php
But I don't want the path in the address bar to change.
Is this possible? I don't understand htaccess very well.
M.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$ [NC]
RewriteRule ^/?$ login.php [L]
This condition RewriteCond %{HTTP_HOST} ^user- is to make sure that rule only impacts a subdomain starting with user-

How to rewrite rules in htacess file

I want to write rules to redirect to following pages using .htaccess file.I am new to this.
How I suppose to do it?
Facebook: test.com/FB (redirects to https://www.facebook.com/test.etutors)
Twitter: test.com/Twitter (redirects to https://twitter.com/test)
Your attempted rule won't work because RewriteRule only matches request URI not the domain part. Have your rules like this in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)test\.com$ [NC]
RewriteRule ^FB/?$ https://www.facebook.com/test.etutors [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)test\.com$ [NC]
RewriteRule ^Twitter/?$ https://twitter.com/test [L,NC,R=301]
PS: Since you're a new user I suggest you to show your code by editing your question rather than in comments.

.htaccess redirect with parameters and multiple directory levels

I need to redirect from this URL:
www.devsite.com/level1/page1.html?brand=6
to:
www.productionsite.com/level1/page1.html?brand=6
I've come across various redirect w/ parameters answers here on stack, but none that specifically address how to rewrite part of .htaccess to redirect to a totally different domain. Help!
Just do a redirect from devsite to productionsite. Second line appends after the devsite domain to the new location.
RewriteCond %{HTTP_HOST} !^www.devsite.com$ [NC]
RewriteRule ^(.*)$ http://www.productionsite.com/$1 [L,R=301]
Add this to the htaccess file in the document root of your www.devsite.com domain. Add it above any other rules that may already be in that file.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?devsite\.com$ [NC]
RewriteRule ^level1/page1\.html$ http://www.productionsite.com/level1/page1.html [L,QSA]
RewriteCond %{QUERY_STRING} \?brand=6
RewriteCond %{HTTP_HOST} ^www\.devsite\.com$
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^level1/page1\.html$ http%2://www.productionsite.com/$0 [R=301,QSA,L]