Redirection to www. using htaccess

General questions relating to Joomla! 1.5 There are other boards for more specific help on Joomla! features and extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Fri Mar 09, 2012 6:39 pm

I want to personally thanks g1smd for is patience.

I realize that I should know more on the Rewrite process and start to study hard yesterday to have some kind of understanding of what I’m doing here.

SITUATION: Joomla files are in a subfolder on the root.

The first thing I did (in my case is to “translate” the code g1smd give me to find my problem —> The site have a correct 301 redirect but link to images, css and other important files are broken.

g1smd advice is :
Your problem sounds like the links to your CSS and JS files are pointing to the wrong location. Use 'view source' to see the code. Be aware that it is the browser that evaluates the URL to be requested for these other resources.

The part of the code that excludes requests for real files from being rewritten might be the culprit. Where are those "real files" located? If those files are also in the subfolder then those requests SHOULD also be rewritten.
Requests for Joomla pages should be rewritten to the index file in the subfolder and requests for real files should be rewritten to their location inside the subfolder. That's two separate rules doing very similar things.

I’m trying hard here : This is what I understand/or not so far

I will put the rest later....

This is only for the first expression:

Code: Select all

# 1. Forward YourSiteName.com to www.YourSiteName.com
RewriteCond %{HTTP_HOST} !^(www\.YourSiteName\.com)?$
Translate; If RewriteCond the host request is %{HTTP_HOST} not ! Starting with^ Make a Back Reference Point matched string ( http://www.YourSiteName.com )—> Match the expression 0 or 1 more times ? ONLY the EXACT match (ending with) (http://www.YourSiteName.com) would qualify$

Plain english: If url is not http://www.YourSiteName.com process with following rule and remember it.

Question: here I don’t understand the point of making a reference point here to remember (www\.YourSiteName\.com) ?
Question: What do you need the exact match ending $ ?

Code: Select all

RewriteRule (.*) http://www.YourSiteName.com/$1 [R=301,L]
Translate; Rewrite - Re-route RewriteRule for (and Make a Back Reference Point matched string) ( Any character . Match indefinite* ) to http://www.YourSiteName.com/add precedent matched string $1 Make a visible url [R Permanent Redirection 301 Last rule in this expression L

Plain English: Take any request and remember it and replace with http://www.YourSiteName.com/ and add what we remember here

Question: As we put the server request to everything, how it make the difference between request for the http://www.YourSiteName.com/ or any other sites on the server ?
Question: When we add the matched string, how come we don’t end up with double url like http://www.YourSiteName.com/http://www. ... m/anything ?
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Fri Mar 09, 2012 8:22 pm

Answers 1, 2 and 3 - The !^(www\.example\.com)?$ pattern matches if the requested hostname is not EXACTLY http://www.example.com AND not blank. The ( ) are NOT there to capture the data for later re-use, they show the part of the code the ? applies to. This condition prevents an infinite redirect loop for pure HTTP/1.0 requests. Pure HTTP/1.0 requests do not include the HOST header in the request.

How this mod-rewite code interacts with other sites on your server depends on how the various folders on the server are assigned to differing hostnames and exactly where the configuration code is placed within the server. If it's one hostname per folder and the config code is in .htaccess then the code can be used as is.

If the config code is located in http.conf, a slightly more complex example is needed:

Code: Select all

# .com canonicalisation
RewriteCond %{HTTP_HOST} example\.com               // CONTAINS "example.com"
RewriteCond %{HTTP_HOST} !^www\.example\.com$       // but NOT EXACTLY "www.example.com"
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# .net canonicaliation
RewriteCond %{HTTP_HOST} example\.net
RewriteCond %{HTTP_HOST} !^www\.example\.net$
RewriteRule (.*) http://www.example.net/$1 [R=301,L]

# .org canonicaliation
RewriteCond %{HTTP_HOST} example\.org
RewriteCond %{HTTP_HOST} !^www\.example\.org$
RewriteRule (.*) http://www.example.org/$1 [R=301,L]
The code now doesn't need the ( )? construct in the second condition per ruleset as the first condition per ruleset matches only if there is a host header present anyway.

This underlines a very important point. mod_rewrite code is rarely "cut and paste" or "one size fits all". It has to be tailored to your server and your site. A small error in the code can take your site immediatly offline (if you are lucky) or present searchengines with errors that cause you to lose rankings and traffic with little or no clue that there's a serious problem (if you're unlucky). It's fair to say that a single mis-placed full stop could put you completely out of business.


Answer 4 - The RewriteRule RegEx pattern looks only at the PATH part of the request. It cannot "see" either the hostname nor the query string. So $1 contains only the path. The rule target therefore explicitly prefixes $1 with the correct target hostname. Additionally, the default action is for the original query string, if present, to be re-appended.
Online since 1995.

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Sat Mar 10, 2012 2:08 pm

Ok
The !^(www\.example\.com)?$ pattern matches if the requested hostname is not EXACTLY http://www.example.com AND not blank.


1— This is for that you need the End with $ - to be sure that the result is not blank?

The ( ) are NOT there to capture the data for later re-use, they show the part of the code the ? applies to. This condition prevents an infinite redirect loop for pure HTTP/1.0 requests. Pure HTTP/1.0 requests do not include the HOST header in the request.

2— How do you make the difference between Grouping and Capture?

Here my answer so far is that it is no difference between Grouping and Capture, anything inside parenthesis are always group/capture — You can always call it back (or not) with the $1. $2 code before any end of rule [L]

How this mod-rewite code interacts with other sites on your server depends on how the various folders on the server are assigned to differing hostnames and exactly where the configuration code is placed within the server. If it's one hostname per folder and the config code is in .htaccess then the code can be used as is.

3— Obviously (sorry for that !) the physical position of the .htacces is primordial and take effect only where present. I guess the effect reach all files and sub-files (in sub-folders) until another .htacces is find (can they oppose or negate each others rules ?? not sure...

Wow ! Your answer 4 is a major highlight — This is major and I yet to see in all beginners and serious tutorial i’m using now that important revelation...

Code: Select all

The RewriteRule RegEx pattern looks only at the PATH part of the request. It cannot "see" either the hostname nor the query string. So $1 contains only the path. The rule target therefore explicitly prefixes $1 with the correct target hostname. Additionally, the default action is for the original query string, if present, to be re-appended.
I just get the book - Definitive Guide to Apache mod_rewrite from Rich Bowen and reading now... I want to understand...


For the next rule, I have another question.

Code: Select all

# 2. Rewrite root request to index file in subfolder
RewriteRule ^$ /subfolder/index.php [L]
Code Translation = Rewrite - Re-route RewriteRule Starting with^ ONLY the EXACT match (ending with)^ to /subfolder/index.php Last rule in this expression L

Plain English: Take any request ( www.YourSiteName.com/whatever) and re-route to http://www.YourSiteName.com/subfolder/index.php

Question 4: Here I don’t understand how we don’t have 404 error when we asking mod_rewrite to re-route www.YourSiteName.com/whatever to http://www.YourSiteName.com/subfolder/index.php. The subfolder redirection is ok but we asking mod_rewrite to replace “whatever” by “index.php”.

I guess this is the part where Joomla SEF motor take over but not sure...
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Sat Mar 10, 2012 9:04 pm

Code: Select all

RewriteRule ^$ /subfolder/index.php [L]
Before being presented to mod_rewrite, any higher level folders and their trailing slash are stripped from the internal pointer. The RewriteRule pattern in that code snippet matches a BLANK and this corresponds only to the URL request www.example.com/ and NOT to any internal pages.

The htaccess files are processed in order from root downwards where the folder names match those in the URL request. Getting the rules in the right order and in the right file can be tricky. It's always easier to have all the rules in the root.
Online since 1995.

User avatar
johans
I've been banned!
Posts: 1293
Joined: Tue Oct 26, 2010 3:54 am
Contact:

Re: Redirection to www. using htaccess

Post by johans » Mon Mar 12, 2012 1:30 am

For me, once joomla is installed i did not change any text on my .htaccess or htaccess.txt

Yes, I agree with the question.
Why do we need to change the .htaccess or htaccess.txt -- in joomla? I need to know also.

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Mon Mar 12, 2012 7:53 am

The default htaccess file contains only a very few basic settings.

It doesn't contain non-www/www canonicalisation code. It's a good idea to add that.

If you're moving a site from some other system to Joomla, you'll need to redirect the old URLs to the new.

There's extra security settings that can be added that give some additional protection against malicious requests.

There are many reasons why you might want to add extra detail to the file.
Online since 1995.

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Tue Mar 13, 2012 1:58 pm

Now the last part ( that don’t work in my case (site appear unformated but good 301).

1— we rewrite www or non www to http://www.YourSiteName.com/ and make it permanent
with:

Code: Select all

RewriteCond %{HTTP_HOST} !^(www\.YourSiteName\.com)?$
RewriteRule (.*) http://www.YourSiteName.com/$1 [R=301,L]
2— We Rewrite root request to the index file in subfolder
with:

Code: Select all

RewriteCond %{HTTP_HOST} !^(www\.YourSiteName\.com)?$
RewriteRule (.*) http://www.YourSiteName.com/$1 [R=301,L]
3— No this is the part where I have a mistake because the site is not retrieving the right path to the images, js and all.

Tell me if I’m right here: (maybe wrong here ;D )
I think that the problem come from the internal redirection.

a) I explain; External redirection (for the index.php and /or probably site pages are correctly handled by the rewrite so far.

b) But internal redirection from Joomla asking for images, js and all end-up to be wrong with the current condition and rules...

c) I see the 4 next Condition to be exception to the last rule that redirect ALL(other than root (already done) request to the subfolder.

Exception 1

Code: Select all

RewriteCond %{REQUEST_URI} !^/subfolder/
Plain English: If the URL end path don’t have the /subfolder/ in go to next rule
This seem to be logical as if you put the subfolder in the path it should go already to the right place

Exception 2 — Where the error is (I think)

# and is not a request that is always handled by a file

Code: Select all

RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|zip|txt)$
Plain English: If the URL end path don’t have the .png, .gif, .jpg .... in go to next rule
Here, I think you don’t want to rewrite those links to be sure that Joomla internal request for link to images, js and all keep their integrity and are real physical path.
I think the problem here is that I don’t include the right path to those files by omitting to indicate mod_rewrite that they are not in the root but in a subfolder.
To fix the problem I think I should add the right path, with the subfolder, to those file to become an exception to the final rule.

That will be my Rewrite Newbie shot at this...
My wild guess will be this modification to the condition

# and is not a request that is always handled by a file

Code: Select all

RewriteCond %{REQUEST_URI} !^/subfolder/.\.(png|gif|jpe?g|css|js|zip|txt)$
Plain English: If the URL end path don’t have the /subfolder/anything.png, .gif, .jpg .... in go to next rule

Exception 3The 2 other exceptions make sure that the physical path to files and folder are not part of the rewrite
# and does not actually exist as a file or folder

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The final rule.
# rewrite the request to be handled by the folder

Code: Select all

RewriteRule (.*) /subfolder/$1 [L]
Rewrite - Re-route RewriteRule Make a Back Reference Point matched string ( Any character . Match indefinite* ) /subfolder/ add precedent matched string $1 Last rule in this expression L

Plain English: Rewrite-Re-route any path after http://www.YourSiteName.com Exept the above exception in the subfolder - Last rule

But I’m probably wrong ....
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Sun Mar 25, 2012 1:21 pm

It work !
Exception 2 — Where the error is (I think)

# and is not a request that is always handled by a file

Code: Select all

RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|zip|txt)$
Plain English: If the URL end path don’t have the .png, .gif, .jpg .... in go to next rule
Here, I think you don’t want to rewrite those links to be sure that Joomla internal request for link to images, js and all keep their integrity and are real physical path.
I think the problem here is that I don’t include the right path to those files by omitting to indicate mod_rewrite that they are not in the root but in a subfolder.
To fix the problem I think I should add the right path, with the subfolder, to those file to become an exception to the final rule.
I replace

Code: Select all

RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|zip|txt)$
by

Code: Select all

# and is not a request that is always handled by a file
RewriteCond %{REQUEST_URI} !(subfolder).\.(png|gif|jpe?g|css|js|zip|txt)$
By adding the subfolder to the path for the rewrite exception it work- Site show images and all + 301 permanent in a subfolder

Is this the right and efficient way to include the subfolder? — I’m not sure?

A big thanks to g1smd..... ;D
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Sun Mar 25, 2012 3:15 pm

Code: Select all

RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|zip|txt)$
means (in the context of the rule that follows this condition) "do not rewrite any URL request that ends with any of the listed extensions". This condition should match for all requests in the root as well as any depth of folder.

It should not be necessary to specify the path details for your subfolder here. It should be included by default. The problem with your site is likely somewhere else in your code.

Perhaps you need

Code: Select all

RewriteCond %{REQUEST_URI} !subfolder/
RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|zip|txt)$
or similar combination.
Online since 1995.

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Mon Mar 26, 2012 6:15 pm

UNFORMATED

Code: Select all

# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.

# Replace YourSiteName.com and /YourSubfolder/ with the real names.

# Redirect to comnonicoml hostname and rewrite to internal YourSubfolder.

# Activate the rewrite engine
RewriteEngine on

# 1. Redirect non-comnonicoml hostame requests to www.YourSiteName.com
RewriteCond %{HTTP_HOST} !^(www\.YourSiteName\.com)?$
RewriteRule (.*) http://www.YourSiteName.com/$1 [R=301,L]

# 2. Rewrite root request to index file in YourSubfolder
RewriteRule ^$ /YourSubfolder/index.php [L]
# RewriteRule !. /YourSubfolder/index.php [L]
# (pick one of the above two lines, they are equivalent)

# 3. Rewrite requests to YourSubfolder
# If the request has not already been rewritten to /YourSubfolder/ 
RewriteCond %{REQUEST_URI} !YourSubfolder/
# and is not a request that is always handled by a file
RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|zip|txt)$
# and does not actually exist as a file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the request to be handled by the folder
RewriteRule (.*) /YourSubfolder/$1 [L]
CORRECTLY FORMATED

Code: Select all

# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.

# Replace YourSiteName.com and /YoureSubfolder/ with the real names.

# Redirect to canonical hostname and rewrite to internal YoureSubfolder.

# Activate the rewrite engine
RewriteEngine on

# 1. Redirect non-canonical hostame requests to www.YourSiteName.com
RewriteCond %{HTTP_HOST} !^(www\.YourSiteName\.com)?$
RewriteRule (.*) http://www.YourSiteName.com/$1 [R=301,L]

# 2. Rewrite root request to index file in YoureSubfolder
RewriteRule ^$ /YoureSubfolder/index.php [L]
# RewriteRule !. /YoureSubfolder/index.php [L]
# (pick one of the above two lines, they are equivalent)

# 3. Rewrite requests to YoureSubfolder
# If the request has not already been rewritten to /YoureSubfolder/ 
RewriteCond %{REQUEST_URI} !^/YoureSubfolder/
# and is not a request that is always handled by a file
RewriteCond %{REQUEST_URI} !(YoureSubfolder).\.(png|gif|jpe?g|css|js|zip|txt)$
# and does not actually exist as a file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the request to be handled by the folder
RewriteRule (.*) /YoureSubfolder/$1 [L]
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Mon Mar 26, 2012 6:57 pm

I think you need something more like this:

Code: Select all

# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.

# Replace example.com and /YourSubfolder/ with the real names.

# Redirect to canonical hostname and rewrite to internal YourSubfolder.

# Activate the rewrite engine
RewriteEngine on

# 1. Redirect non-canonical hostame requests to www.example.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# 2. Rewrite root request to index file in YourSubfolder
RewriteRule ^$ /YourSubfolder/index.php [L]
# RewriteRule !. /YourSubfolder/index.php [L]
# (pick one of the above two lines, they are equivalent)

# 3. Rewrite requests to the Joomla index.php file in YourSubfolder
# If the request has not already been rewritten to /YourSubfolder/
RewriteCond %{REQUEST_URI} !^/YourSubfolder/
# and is not a request that is always handled by a file
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw|ini|zip|json|file))$ [NC]
# and does not actually exist as a file or folder
RewriteCond /YourSubfolder/%{REQUEST_FILENAME} !-f   // not sure if the /YourSubfolder/ part
RewriteCond /YourSubfolder/%{REQUEST_FILENAME} !-d   // is needed in these two conditions or not
# rewrite the request to be handled by the Joomla index.php file in the subfolder
RewriteRule .* /YourSubfolder/index.php [L]

# 4. Rewrite all other requests to YourSubfolder
# If the request has not already been rewritten to /YourSubfolder/
RewriteCond %{REQUEST_URI} !^/YourSubfolder/
# rewrite the request to be handled by the folder
RewriteRule (.*) /YourSubfolder/$1 [L]
Note the extra section.
Online since 1995.

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Tue Mar 27, 2012 12:05 pm

Odd !!!
- This give me a 500 internal error with or without the subfolder in the 2 line (!-f and !-d )
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Tue Mar 27, 2012 8:11 pm

There's a minor typo or logic error somewhere in there, but I can't see it.

What I think you want to do is rewrite requests for Joomla pages to the index file in the subfolder and rewrite other requests to the real files in the subfolder.

That's why separate rules are needed.
Online since 1995.

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Wed Mar 28, 2012 8:08 am

Have you made all of the changes to section 3 as shown above?

Several lines in section 3 are changed compared to your earlier version.

Section 4 is new, but uses one of the lines previously found in section 3.
Online since 1995.

PierreB
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 152
Joined: Tue Sep 21, 2010 2:39 pm

Re: Redirection to www. using htaccess

Post by PierreB » Fri Apr 06, 2012 11:02 am

I'm having some trouble I would really appreciate help for...

I have recently changed my aliases from unicode to non-unicode and I've been getting many, many, many references on my "Redirect" component for an extension I had loading videos from [youtube] feeds...

The prior URL looked like this

Code: Select all

http://mysite.gr/βίντεο?ufeedpage=1&mid=193&ncache=000044365
Now it has been changed to

Code: Select all

http://mysite.gr/video?ufeedpage=1&mid=193&ncache=000044365
- This part "1&mid" tells what page of the feeds the user is on (right now I have 7 pages of feeds)
- This part "193&ncache" is always the same
- and this part "000044365" can include any set of numbers of nine digits

is there a way to have all references redirected from

Code: Select all

http://mysite.gr/βίντεο?ufeedpage=X&mid=193&ncache=XYZXYZXYZ
to

Code: Select all

http://mysite.gr/video?ufeedpage=X&mid=193&ncache=XYZXYZXYZ
with an htaccess rule?

I'm already in the area of seventeen thousand IDs in the "Redirect" component, it just seems this will go on until all the possible combination of digits, indexed by google, will register as 404 links....

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Fri Apr 06, 2012 7:32 pm

The breaks are not 1&mid and 193&ncache

They are:
ufeedpage=X
mid=193
ncache=XYZXYZXYZ

Use a RewriteCond looking at %{QUERY_STRING} before your redirecting RewriteRule.
Online since 1995.

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Wed Nov 07, 2012 7:29 pm

Can't tell you what is wrong with your domain canonicalisation redirect code if you don't post it.

However, there's penty of threads with example code for this throughout the forum.

Rule order is very important. Redirects must be listed before rewrites.

Additionally,

Code: Select all

RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
simplifies to

Code: Select all

RewriteCond $1 !^(index\.php|(rpc_relay|canvas)\.html|robots\.txt|sitemap\.xml|system|export)
and don't forget to escape literal periods in RegEx patterns.
Online since 1995.

PierreB
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 152
Joined: Tue Sep 21, 2010 2:39 pm

Re: Redirection to www. using htaccess

Post by PierreB » Tue Nov 20, 2012 10:49 am

Hi @g1smd, sorry for bothering you.

I would like your opinion concerning the "suggested master htaccess file" (unfortunately that discussion thread was closed)...

Is it worth it adding all that security related code to the htaccess file, specifically the "block bad user agents" and "advanced server protection" blocs? Is there a performance trade-off for adding those extra rules and if so, is it worth it?
Generally, what would you regard as more valuable from that suggested code for performance/security?

Some of the code in the advanced server protection (the following) caused me some trouble with one extension and the developer kind of dismissed the purpose of those security rules, claiming that Joomla does a good enough job of protecting its system files, and if one were to actually engage in attacks against you, there are far more efficient ways to do it, than those which the following code protects you from...
...what's your take?

Code: Select all

## Allow limited access for certain Joomla! system directories with client-accessible content
RewriteRule ^(components|modules|plugins|templates)/([^/]+/)*([^/.]+\.)+(jp(e?g|2)?|png|gif|bmp|css|js|swf|html?|mp(eg?|[34])|avi|wav|og[gv]|xlsx?|docx?|pptx?|zip|rar|pdf|xps|txt|7z|svg|od[tsp]|flv|mov)$ - [L]
## Uncomment this line if you have extensions which require direct access to their own
## custom index.php files. Note that this is UNSAFE and the developer should be ashamed
## for being so lame, lazy and security unconscious.
# RewriteRule ^(components|modules|plugins|templates)/([^/]+/)*(index\.php)?$ - [L]
## Uncomment the following line if your template requires direct access to PHP files
## inside its directory, e.g. GZip compressed copies of its CSS files
RewriteRule ^templates/([^/]+/)*([^/.]+\.)+php$ - [L]
RewriteRule ^(components|modules|plugins|templates)/ - [F]


Also, I see in that suggested file the following code, for the automatic compression of resources, which at the same time is declared "deprecated", if I understand the comments correctly

Code: Select all

########## Begin - Automatic compression of resources
# Compress text, html, javascript, css, xml, kudos to Komra.de
# May kill access to your site for old versions of Internet Explorer
# The server needs to be compiled with mod_deflate otherwise it will send HTTP 500 Error.
# mod_deflate is not available on Apache 1.x series. Can only be used with Apache 2.x server.
# AddOutputFilterByType is now deprecated by Apache. Use mod_filter in the future.
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript
########## End - Automatic compression of resources
I have run some tests and this seems to work better than the Joomla Gzip compression, but is there a more efficient way to run it?


Cheers

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: Redirection to www. using htaccess

Post by g1smd » Tue Nov 20, 2012 11:41 am

It protects against some specific attacks and flaws. It's still a long way from perfect. Server overhead is minimal for most of the rules.

It's for you to work out which you want to incorporate and which are compatible with your hosting service. There's too many variables, it's never a cut and paste solution.

As for compression, again, test to see which works best for you. I'm not an expert on all the various ways of compressing things. There's a lot of choice.
Online since 1995.

hirudo
I've been banned!
Posts: 24
Joined: Sat Apr 13, 2013 11:40 am

Re: Redirection to www. using htaccess

Post by hirudo » Tue Apr 30, 2013 8:41 am

Your information is very useful
If you want to link with www. that I'd better introduce myself?

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Wed Oct 30, 2013 11:26 pm

Have to come back here

Joomla 2.5

Need to redirect www to non-www this time


this is the non-www to www example

Code: Select all

## Begin - Custom redirects
#
########## Begin - 301 Redirect
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|home)\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)(index|home)\.html?$ http://www.YOURDOMAIN.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^(([^/]+/)*)index\.php$ http%2://www.YOURDOMAIN.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^(www\.YOURDOMAIN\.com)?$
RewriteRule (.*) http://www.YOURDOMAIN.com/$1 [R=301,L]
#
## End - Custom redirects

this is the www to non-www example

## Begin -Custom redirects WWW to non-WWW
#
########## Begin - 301 Redirect
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(index|home)\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)(index|home)\.html?$ http://www.YOURDOMAIN.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^(([^/]+/)*)index\.php$ http%2://www.YOURDOMAIN.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^(yourdomain\.com)?$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]
#
## End - Custom redirects
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

knalim
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 143
Joined: Wed Jan 01, 2014 11:39 pm
Location: SLOVAKIA
Contact:

Re: Redirection to www. using htaccess

Post by knalim » Mon Jan 13, 2014 5:55 pm

Hi everyone, you look to be professional in joomla. I have similar problem like here..

My restored site is linked to my old web. If I put www.primaclima.sk - there is old web.
If I put http://primaclima.sk or www2.primaclima.sk - I get my new site displayed

Here is my topic:
http://forum.joomla.org/viewtopic.php?f ... 3#p3127103

Could you please help me to launch my site properly?

THANK YOU VERY MUCH

Milan
Joomla amateur but Enthusiast and Lover :)
Attendant of J and Beyond 2015 in Prague
Im beekeeper, I love honey and bees :) http://www.medaren.sk

kardinol
I've been banned!
Posts: 29
Joined: Sun Aug 18, 2013 11:23 am

Re: Redirection to www. using htaccess

Post by kardinol » Fri May 30, 2014 2:21 pm

I want www mysite com to go to mysite.com as normal in .htaccess for seo and other reasons.
I have searched the web all over for a good generic .htaccess script for redirecting non-www to www, and currently i'm using this:

Code: Select all

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This works fine, but if i go to a subdomain www. will be added. Does anyone has a good working redirect .htaccess script?

User avatar
dpacadmin
Joomla! Champion
Joomla! Champion
Posts: 6029
Joined: Sat Aug 16, 2008 1:46 pm
Location: the Bat Cave
Contact:

Re: Redirection to www. using htaccess

Post by dpacadmin » Fri May 30, 2014 6:01 pm

I found a redirect plugin here for all versions of Joomla;
http://www.algisinfo.com/en/download/ca ... sions.html

dorsa
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Mon Jul 29, 2013 5:46 am

Re: Redirection to www. using htaccess

Post by dorsa » Tue Jul 29, 2014 2:48 pm

@dpacadmin, This plug-in will apply to all browsers?
How can I get more information about it?

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Wed Jul 30, 2014 1:09 pm

@Knalim
Normally you will be using
public $live_site = 'http://primaclima.sk'; in the configuration.php file
and the —. www to non-www example I show up there.
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

kardinol
I've been banned!
Posts: 29
Joined: Sun Aug 18, 2013 11:23 am

Re: Redirection to www. using htaccess

Post by kardinol » Wed Jul 30, 2014 7:47 pm

My joomla site is set up so that any time you remove the www from the url, it will take to back to the home page.

For SSL reasons, I need a single page to be accessed without the www.

Lets call it test.org/page

Is there a custom redirect I can put in my htaccess, so that

Code: Select all

http://www.test.org/page
is redirected to test.org/page?

User avatar
Chacapamac
Joomla! Ace
Joomla! Ace
Posts: 1088
Joined: Wed Feb 20, 2008 6:50 am
Location: Canada, Montreal
Contact:

Re: Redirection to www. using htaccess

Post by Chacapamac » Wed Jul 30, 2014 8:00 pm

I think you should be able to do that. It’s probably really easy for somebody that really know regex.
And I’m not a pro. If you google —> redirect a www url to a non www url you find some infos

Otherwise you can do that probably with the renown extension component ReDJ —> http://extensions.joomla.org/extensions ... ction/7189
The Dev’s are nice…
Probably easy but I’m sure with a little research you should find the cleaner htaccess way.
Can God help us?
Marketing, SEO, Web development - Powered by Joomla!
http://www.grafcomm.ca/

planetafm
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon Aug 04, 2014 10:53 pm

Re: Redirection to www. using htaccess

Post by planetafm » Mon Aug 04, 2014 11:11 pm

Sorry for my english ...

I need help. How to set route .Htaccess one interval schedule in which the site is redirected to another page ...
Imagine that the homepage is: http://xxxxxxxxa.org/home/ and I want, for example, between 12 and 13 appear to be the main "http://xxxxxxxxa.org/home/alternative/"

Some kind soul help me?
Hugs to all

Queasy
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 155
Joined: Thu Apr 17, 2014 12:50 am
Location: Portland Main
Contact:

Re: Redirection to www. using htaccess

Post by Queasy » Tue Aug 05, 2014 12:55 am

You can also just use SH404SEF extension and be done with it in just seconds. Nice extension.


Locked

Return to “General Questions/New to Joomla! 1.5”