Redirect example.com/new-section/ url to www.example.com/new-section/

I have some issues with my .htaccess files and my developer claims it’s not possible what I want to achieve but I think it’s just an attempt of avoiding more work/or he doesn’t have the knowledge how to do it himself. So basically I wanted to ask around and see if this really is impossible?

My website is divided in two different sections. There is the main page: http://www.example.com/ and then there is the new section (http://www.example.com/new-section/)

I want http://example.com/new-section/ to redirect to http://www.example.com/new-section/

How would I go ahead doing that?

Please see my two htaccess files below.

.htaccess file for root (www.example.com):


Options +FollowSymLinks
Options +Indexes
RewriteEngine On

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

RewriteCond %{REQUEST_URI} !\\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L]

RewriteRule ^pages/([0-9]+)/(.*)$ index.php?page_id=$1&keyword=$2

ErrorDocument 404 http://www.example.com/404page.php

.htaccess file for sub directory (www.example.com/new-section/):


Options +FollowSymLinks
Options +Indexes
RewriteEngine On

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

RewriteCond %{REQUEST_URI} !\\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/new-section/$1/ [R=301,L]

RewriteRule ^login/$ login.php
RewriteRule ^contact/$ contact.php
RewriteRule ^help/$ help.php

ErrorDocument 404 http://www.example.com/404page.php

I would really appreciate your help. :slight_smile:

It’s definitely possible. It looks like you’re almost doing it already.

[FONT=Courier New]# /new-section/.htaccess

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

The only reason this might not work properly is because the “new-section” path segment won’t make it into the replacement URL. Rewrite rules match on a relative path, relative to the current directory of the htaccess file, so $1 won’t contain any preceding parts. You’ll have to write it in manually.

[FONT=Courier New]# /new-section/.htaccess

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/new-section/$1 [R=301,L][/FONT]

I haven’t tested it, but I believe that should solve the immediate issue.

Going forward, I’d also suggest trying to consolidate the two htaccess files down to just the one in the root. Personally, I find that much easier to manage and maintain. Already, you can see lots of duplicated code between the two files.

help,

Simply test for non-www AND new-section before your other mod_rewrite code (after RewriteEngine on, of course) IN THE DocumentRoot. Note that this will cause a loop with your force-www code so merely exclude the new-section there, too. Don’t forget your No Case flags!

.htaccess file for root (www.example.com):


Options +FollowSymLinks
Options +Indexes
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\\. [NC]
RewriteCond %{REQUEST_URI} ^new-section/
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteCond %{REQUEST_URI} !^new-section/
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

...

Regards,

DK

I did try your examples but it’s quite confusing but I’ll try some more later. I too think it would be beneficial only having one .htaccess file and that’s what I believe most websites use.

help,

Sorry for the overly detailed explanation using your own code.

To summarize, you’ve use .htaccess files in both directories whereas I prefer (not necessary, of course) to use only one. It looks like you’ve seen this, too.

In both tasks (force www on the subdirectory and force non-www on non-subdirectory) where you’re checking ONLY the www in the {HTTP_HOST}. Clearly, for what you’re trying to do (force one in one case, the other in the other case), you must check both the {HTTP_HOST} and {REQUEST_URI}.

Simple (and clear) enough?

Regards,

DK

Yeah, I get what you saying now but I think I’ll need a skilled htaccess programmer to sort this out, though. A little above my current skills and I don’t wanna break anything.

help,

Aw, really?

First, replace your current DocumentRoot’s .htaccess with

# Delete this line as it should already be in the .htaccess file
# Options +FollowSymLinks
# NOT a good idea to show your file structure so change
# Options +Indexes to
Options -Indexes
# move to top where (technique-wise) all core directive should reside
ErrorDocument 404 http://www.example.com/404page.php

RewriteEngine on

# force www on new-section only
RewriteCond %{HTTP_HOST} !^www\\. [NC]
RewriteCond %{REQUEST_URI} ^new-section/
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# force non-www on !new-section requests
RewriteCond %{HTTP_HOST} ^www\\. [NC]
RewriteCond %{REQUEST_URI} !^new-section/
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

# force trailing /'s??? IMHO, VERY bad thing to do!
# Don't comment this section out IF you need the trailing /'s for a VALID reason
# RewriteCond %{REQUEST_URI} !\\.[^./]+$
# RewriteCond %{REQUEST_URI} !(.*)/$
# RewriteCond %{REQUEST_URI} !new-section/ # added to prevent hijacking a new-section/ request
# RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L]

# normally, you should be checking for whether the {REQUEST_URI}
# exists as either a file or directory, but ...
RewriteRule ^pages/([0-9]+)/(.*)$ index.php?page_id=$1&keyword=$2 [L]

Then delete your .htaccess in the new-section folder.

Caveat Emptor: I don’t know what your intent is with some of this code but, with my interpretation, I am offering this as I believe it’s what you’ve asked for.

Regards,

DK