Page 1 of 1

301 Redirects and Pretty URls - and folders

Posted: Thu Dec 19, 2013 9:48 pm
by tophers
I have read quite a few posts on this topic, and seen more than a few 'solutions', but nothing seems to be working for me, or addressing my particular issues.

I have a new site built in CMSMS that has consolidated 6 old hard-coded sites which all previously sat in subfolders on the main site (;http://www.myurl.com/directory1), ;http://www.myurl.com/directory2, etc.). I need to keep links to particular files, as well as to generally redirect folders

I have tried using:

Code: Select all

RewriteRule ^directory1/pagename.php$ http://www.myurl.com/newdirectory/pagename [NC,R=301,L]
As well as:

Code: Select all

RewriteRule ^directory1/pagename.php$ http://www.myurl.com/index.php?page=pagename [NC,R=301,L]
Neither worked. But this did:

Code: Select all

RedirectMatch 301 /directory1/pagename.php http://www.myurl.com/index.php?page=pagename
Which will be fine for individual pages. I'm still unsure as to why the suggested methods aren't working, but I will take what I can get.

Now on to directories. I need to redirect all traffic (except for the specific examples like those above) to the root URL, so that
;http://www.myurl.com/directory1/anysub/pagename
goes to
;http://www.myurl.com

The suggested method says to use:

Code: Select all

RewriteRule ^directory1/(/?)$ http://www.myurl.com [R=301,L]
But like the method for individual pages, this one fails too. Any ideas?

PS - my CMS and modules are all up to date. And here's my .htaccess file:

Code: Select all

# Attempt to override some php settings, these settings may be helpful on some hosts if your
# default configuration does not meet CMS's minimum requirements, and your host
# has given your account appropriate permissions
#php_value upload_max_filesize "10M"
#php_value session_save_path "tmp/cache"

#php_flag magic_quotes_gpc Off
#php_flag register_globals Off
#php_flag session.use_trans_sid Off

# This is important, so uncomment if your host permit
#Options -Indexes
#ServerSignature Off
#php_value session.cookie_httponly true

#Options +FollowSymLinks

# To prevent E_STRICT problems with PHP 5.3+ you can uncomment the following lines
# Note: These settings should only be enabled for production sites!
#php_flag display_startup_errors 0
#php_flag display_errors 0
#php_flag html_errors 0
#php_value docref_root 0
#php_value docref_ext 0

<IfModule mod_rewrite.c>
RewriteEngine on
#
#Sub-dir e.g: /cmsms
RewriteBase /

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# but ignore POST requests.
#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
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

# Rewrites urls to use www
#
RewriteCond %{HTTP_HOST} ^myurl.com$
RewriteRule (.*) http://www.myurl.com/$1 [R=301,L]

# Redirect Single Web Pages
#
RedirectMatch 301 /directory1/oldpagename.php http://www.myurl.com/index.php?page=pagename
etc.
...

</IfModule>

<IfModule mod_headers.c>
# Disable ETags
Header unset ETag
FileEtag None
# For Security
Header set X-Frame-Options "SAMEORIGIN"
</IfModule>

<IfModule mod_deflate.c>
# Compress css, plaintext, xml, gif, and images in transport.
AddOutputFilterByType DEFLATE text/css text/plain text/xml image/gif image/jpeg image/png
</IfModule>

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
# Set expires tags on various file types... so that the browser wont attempt to reload them.
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType video/x-flv "access plus 1 year"
ExpiresByType application/pdf "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access plus 1 year"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/css "access plug 1 month"
<IfModule mod_headers.c>
  # Setting cache control to public allows proxy servers to cache the items too.
  Header set Cache-Control "public"
</IfModule>
</IfModule>

Re: 301 Redirects and Pretty URls - and folders

Posted: Fri Dec 20, 2013 5:51 pm
by velden
RewriteRules are powerful but complex. You should understand regular expressions to know why something does (not) work.

For example: a dot is a special character in the regex. You should escape it with an escape character if you're looking for a literal dot.

If there is no logic in the old-new url's then I'd consider using the redirect and not redirectMatch (never used them though): http://www.askapache.com/htaccess/301-r ... match.html

I don't understand why this would work:

Code: Select all

RewriteRule ^directory1/(/?)$ http://www.myurl.com [R=301,L]
Maybe this does

Code: Select all

RewriteRule ^directory1/ http://www.myurl.com [R=301,L]
Just check if the request starts with 'directory1/'. The rest is irrelevant as you will redirect it anyway.

Re: 301 Redirects and Pretty URls - and folders

Posted: Fri Dec 20, 2013 9:34 pm
by calguy1000
1. Order of rules is important with rewrite rules. Pay attention to that
2. the L rule implies Last... i.e: no further translation will be performed if the conditions match. Gotta pay attention to that too.
3. Also Pay attention to the !-f and !-d rules
they specifically say IF THE MATCH is not a file/directory THEN

So, by reading this .htaccess file it states that
you should rewrite stuff in the form of /path1/path2/file.ext to
/index.php?page=path1/path2/file.ext IF it's not a real file or directory.

I bet that you still have the old directories existing.