htaccess 301 rewrite old static to new site with pretty urls

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

htaccess 301 rewrite old static to new site with pretty urls

Post by howey »

Hi

I have an old static site that I have converted to CMSMS, but I want to make sure book marked pages etc are directed to the new dynamic address

Not sure how to do this, I found some information that said put the 301 redirect in the htaccess file as below

redirect 301 /oldpage.htm "http://www.domain.co.uk/newpage.html

However, this doesn't seem to work. As the urls are dynamic and I have pretty urls. All I get is the url written as:

"http://www.domain.co.uk/newpage.html?page=oldpage.htm

also, as I am directing to pages that are under a section header do I need to account for this? ie

Old static domain: "http://www.domain.co.uk/page.htm
New page url on CMSMS site using pretty urls:
"http://www.domain.co.uk/sectionheader/newpage.html

Not sure if this is the right place to ask this, but it is installation using the supplied htaccess plus the redirect lines.

PS: I also tried

Code: Select all

RewriteRule ^something.html$ http://www.website.com/new/path/to/something [R=301,L]

and

[code]RewriteRule something.html$ http://www.website.com/new/path/to/something [R=301,L]
Last edited by Dr.CSS on Fri Aug 30, 2013 5:14 pm, edited 1 time in total.
Reason: Please use double quotes on fake links so they aren't clickable...
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: htaccess 301 rewrite old static to new site with pretty

Post by Rolf »

- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

Re: htaccess 301 rewrite old static to new site with pretty

Post by howey »

Hi yes I tried the rewrite rules shown there.

Code: Select all

    
RewriteRule ^old/path/to/something.html$ http://www.website.com/new/path/to/something [R=301,L]
RewriteRule ^something$ http://www.website.com/something [R=301,L]
RewriteRule ^something/$ http://www.website.com/something/ [R=301,L]
RewriteRule something.html$ http://www.website.com/something [R=301,L]
But still returned the url

Code: Select all

http://www.domain.co.uk/newpage.html?page=oldpage.htm
I'm not sure if I've either got the syntax wrong, or the position in the htaccess file wrong.

the old page is

Code: Select all

www.domain.co.uk/page.htm
the new page is under a section header and through pretty urls reads

Code: Select all

www.domain.co.uk/sectionheader/newpage.html
I have activate the pretty urls mod_rewrite and the page extension .html
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: htaccess 301 rewrite old static to new site with pretty

Post by velden »

I don't see a rule that ends on 'htm' while you type that's the 'extension' the old page is called.
Then one rule that has '.html' in it, does not have the dot escaped which you probably want. It should however work because the dot stands for any character in regex,

Further, if all news pages are under the same section header you could even use a generic rule like:

Code: Select all

RewriteRule ^(.*\.htm)l$ http://www.website.com/sectionheader/$1 [R=301,L]
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

Re: htaccess 301 rewrite old static to new site with pretty

Post by howey »

Hi

This is what I have included in the htacess file

Code: Select all


RewriteRule pressure.htm$ http://www.pressure-vessels.co.uk/pressure-vessels-and-systems/pressure-vessels.html [R=301,L]

If you input the link

Code: Select all

http://www.pressure-vessels.co.uk/pressure.htm
It returns a 404 URL not found
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: htaccess 301 rewrite old static to new site with pretty

Post by velden »

Maybe you want to post the whole .htaccess file contents
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

Re: htaccess 301 rewrite old static to new site with pretty

Post by howey »

Hi

I got round the initial problem by creating internal links and ceating pages within CMS to generate the appropriate url - changing the .html to .htm in the config. I only had 4 pages to redirect.

However, I would really like to know how to do this with the htaccess file.

The file I used is below, basically the generic file with a line of code added to the bottom

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]
</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>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
# Set expires tags on various files... so that the browser wont attempt to reload them.
ExpiresActive On
ExpiresDefault "access plus 1 year"
<IfModule mod_headers.c>
  # Setting cache control to public allows proxy servers to cache the items too.
  Header set Cache-Control "public"
</IfModule>
</FilesMatch>
</IfModule>


RewriteRule pressure.htm$ http://www.pressure-vessels.co.uk/pressure-vessels-and-systems/pressure-vessels.html [R=301,L]
Any help would be appreciated. The more I learn, the better I am. I subscribe to the old maxim "learn something new everyday".

Thanks
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: htaccess 301 rewrite old static to new site with pretty

Post by velden »

mmm, you see a <IfModule mod_rewrite.c> ... </IfModule> block with a the conditions and rules and then decide to enter your rule outside that block... Why would you do that?

Then the rules are applied in the order they appear. You can imagine that you rule never will apply as all (almost) all url's are already rewritten by RewriteRule ^(.+)$ index.php?page=$1 [QSA]

I (not being mod_rewrite expert) would suggest to try:

Code: Select all

<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 pressure.htm$ http://www.pressure-vessels.co.uk/pressure-vessels-and-systems/pressure-vessels.html [R=301,L]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
howey
Forum Members
Forum Members
Posts: 158
Joined: Fri Sep 14, 2007 1:05 pm

Re: htaccess 301 rewrite old static to new site with pretty

Post by howey »

Hi

Thanks for that. I'll try it out and see what happens.

Not been an expert I just followed instructions I found while searching, which said to put the rule at the bottom of the ht file. Probably works if you don't have anything else in there.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: htaccess 301 rewrite old static to new site with pretty

Post by calguy1000 »

time to actually read and understand what you're doing instead of just stabbing blindly till something works:

http://httpd.apache.org/docs/current/mo ... ewriterule
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Post Reply

Return to “The Lounge”