[SOLVED] - 301 Redirect Root URL

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
User avatar
richbothe
Forum Members
Forum Members
Posts: 95
Joined: Tue Aug 15, 2006 5:34 pm

[SOLVED] - 301 Redirect Root URL

Post by richbothe »

CMSMS Ver. 1.6.6

Whenever I enable this rewrite rule in my .htaccess file the redirect works, but then forms on my site don't work.

Code: Select all

#RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
#RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Is there a better way to force it so that http://domain.com redirects to http://www.domain.com?  I know enough about .htaccess files to write one with the help of a tutorial, but not much more.

Here a sample of my complete .htaccess file.  Any help/suggestions to make this better is much appreciated.

Code: Select all

#php_flag magic_quotes_gpc Off
#php_flag register_globals Off
#php_flag session.use_trans_sid Off
#php_value upload_max_filesize 16M
#php_value memory_limit 16M

# Make sure you have Options FollowSymLinks
# and Allow on

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Rewrites page.shtml as index.php?page
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

#AuthType Basic
#AuthName "Password Required"
#AuthUserFile /aomam.org/httpdocs/auth/.passwd
#AuthGroupFile /aomam.org/httpdocs/auth/group.file
#require valid-user
#Require Group admins

#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
#RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Rich
Last edited by richbothe on Wed Nov 11, 2009 7:20 pm, edited 1 time in total.
Sonya

Re: 301 Redirect Root URL

Post by Sonya »

Forms often do not work because of the trailing slash condition:

Code: Select all

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
User avatar
richbothe
Forum Members
Forum Members
Posts: 95
Joined: Tue Aug 15, 2006 5:34 pm

Re: 301 Redirect Root URL

Post by richbothe »

Turning off the trailing slash condition seems to make no difference as the forms still don't work.

Code: Select all

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
#RewriteCond %{REQUEST_URI} !/$
#RewriteCond %{REQUEST_URI} !\.
#RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
Sonya

Re: 301 Redirect Root URL

Post by Sonya »

For my sites I always use:

Code: Select all

# Redirect to canonical domain name with www
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*) http://www.yourdomain.com/$1 [L,R=301]
The only difference in your version is different usage of the end delimiter $ and missing escape in the first line:

Code: Select all

RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Try this, it sure works with every form I have ever tested.
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: 301 Redirect Root URL

Post by Rolf »

Whenever I enable this rewrite rule in my .htaccess file the redirect works, but then forms on my site don't work.
This is a known bug, add this into your htaccess.

Code: Select all

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# except for form POSTS             <--------------------------
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$             <--------------------------------
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
Regards, Rolf
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
richbothe
Forum Members
Forum Members
Posts: 95
Joined: Tue Aug 15, 2006 5:34 pm

Re: 301 Redirect Root URL

Post by richbothe »

Hmm... Still not working.  And because my forms don't work this also prevents me from logging into CMS Admin.  Here is my revised .htaccess file.

Code: Select all

# Make sure you have Options FollowSymLinks
# and Allow on

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Rewrites page.shtml as index.php?page
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# except for form POSTS 
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$ 
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

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

Re: 301 Redirect Root URL

Post by Sonya »

richbothe wrote: Hmm... Still not working.  And because my forms don't work this also prevents me from logging into CMS Admin.  Here is my revised .htaccess file.
Rename your .htaccess file and try to login in admin without. If it does not work, then you do not have any problem with forms but with your installation in general.

Explain exactly: "forms do not work". Do you get an error?
User avatar
richbothe
Forum Members
Forum Members
Posts: 95
Joined: Tue Aug 15, 2006 5:34 pm

Re: 301 Redirect Root URL

Post by richbothe »

Renaming .htaccess allows me to login to admin, but then causes a 404 error for all my pages except for my home page.  I have "mod_write" enabled for pretty urls.
JeremyBASS

Re: 301 Redirect Root URL

Post by JeremyBASS »

richbothe wrote: Hmm... Still not working.  And because my forms don't work this also prevents me from logging into CMS Admin.  Here is my revised .htaccess file.

Code: Select all

# Make sure you have Options FollowSymLinks
# and Allow on

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Rewrites page.shtml as index.php?page
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# except for form POSTS 
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$ 
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

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

try this too..

Code: Select all

# BEGIN Optional settings

# Turns off directory browsing
# not absolutely essential, but keeps people from snooping around without 
# needing empty index.html files everywhere
Options -Indexes

# Deny access to config.php
# This can be useful if php ever breaks or dies
# Use with caution, this may break other functions of CMSms that use a config.php
# file.  This may also break other programs you have running under your CMSms
# install that use config.php.  You may need to add another .htaccess file to those
# directories to specifically allow config.php.
<Files "config.php">
order allow,deny
deny from all
</Files>

# Sets your 403 error document
# not absolutely essential to have, 
# or you may already have error pages defined elsewhere
ErrorDocument 403 /forbidden403.shtml

# No sense advertising what we are running
ServerSignature Off

# END Optional Settings

# BEGIN CMSMS and Rewrite Rules
# Make sure you have Options FollowSymLinks
# and Allow on

RewriteEngine On

# Might be needed in a subdirectory
#RewriteBase /
# URL Filtering helps stop some hack attempts
#IF the URI contains a "http:"
RewriteCond %{QUERY_STRING} http\: [OR]

#OR if the URI contains a "["
RewriteCond %{QUERY_STRING} \[ [OR]

#OR if the URI contains a "]"
RewriteCond %{QUERY_STRING} \] [OR]

#OR if the URI contains a "<__script__>"
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]

#OR script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]

#OR any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [OR]

#IF the URI contains UNION
RewriteCond %{QUERY_STRING} UNION [OR]

#OR if the URI contains a *
RewriteCond %{QUERY_STRING} \*

#then deny the request (403)
RewriteRule ^.*$ - [F,L]

# End URL Filtering

# force CMSMS www IN URL 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
# END CMSMS force www IN URL 


# CMSMS Rewriting
# Set assume mod_rewrite to true in config.php and clear CMSMS cache
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

This has worked on 99% of all servers I've placed it on... Hope that helps as you were forcing the www after the pretty url and doing the pretty url twice..

Cheers
Jeremy
User avatar
richbothe
Forum Members
Forum Members
Posts: 95
Joined: Tue Aug 15, 2006 5:34 pm

Re: 301 Redirect Root URL

Post by richbothe »

I changed .htaccess to the code you provided Jeremy (thanks), and that solved the 404 error, but still forms not working and can't login to admin.

The website is http://theplaygroundent.com and the admin is http://theplaygroundent.com/eadmincp.

I setup a demo user login that you can use to see what's happening.

UN: demouser
PW: password
JeremyBASS

Re: 301 Redirect Root URL

Post by JeremyBASS »

first.. version? if all up to date .. then... as a test of to the login.php (ie: admin/themes/NCleanGrey/login.php) and change the



to



but..

also insure your config file is right...

#Name of the admin directory
$config['admin_dir'] = 'eadmincp';


since your useing that eadmincp.
JeremyBASS

Re: 301 Redirect Root URL

Post by JeremyBASS »

in fact I just tested that.. and that change logs you in...
Sonya

Re: 301 Redirect Root URL

Post by Sonya »

Well, simple solution:

Open the file config.php and change

Code: Select all

$config['root_url'] = 'http://theplaygroundent.com';
to

Code: Select all

$config['root_url'] = 'http://www.theplaygroundent.com';
That is :)
User avatar
richbothe
Forum Members
Forum Members
Posts: 95
Joined: Tue Aug 15, 2006 5:34 pm

Re: 301 Redirect Root URL

Post by richbothe »

I made the change to the config file and that did it!  Works like a charm now.  Thank you both for your help.  I've been having this issue on other sites for a little while and just never felt like dealing with it.  Cheers!
Post Reply

Return to “Modules/Add-Ons”