2

I just redeveloped an existing site from the ground up. The old site used pure HTML while the new site will draw content from a database. Now I need to redirect the old pages to the new URL structure, but I’m a bit confused about how to write the .htaccess rules for a file-to-file redirect.

In all the examples I’ve found, the first part of the rule looks like an absolute directory path from the root, but they only contain the part of the URL that immediately follows the domain name.

For instance, I want to redirect

https://garrettcounty.us/archives/12262011news.html

to

https://garrettcounty.us/news/20111226/house-fire-on-christmas-day

From the examples I’ve seen (both on StackOverflow and abroad), I guess the rule would be

redirect 301 /archives/12262011news.html https://garrettcounty.us/news/20111226/house-fire-on-christmas-day

but the actual path to the original file on the server is

/home/username/public_html/archives/12262011news.html

Should I use the directory path or the path from the domain?

I would love to be able to use a rewrite rule. Unfortunately, the original developer didn’t use a consistent file naming scheme so I’m faced with things like

12262011news.html
Jan-19-2012-Headlines.html
State-Of-The-Union-25jan2012.html

In the new model, I'm directing everything through index.php with

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

so if anyone knows of an easier way to map all the old pages to the new URLs, I’d love to hear it. As it stands, it looks like I have to redirect 70+ pages one-by-one.

Herbert
  • 5,698
  • 2
  • 26
  • 34

1 Answers1

2

Well you old URL doesn't have news title so obviously mod_rewrite cannot create it. However to redirect

https://garrettcounty.us/archives/12262011news.html

to

https://garrettcounty.us/news/20111226/

you can use code like this in your .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} =on
RewriteRule ^archives/(\d+)([^.]*)\.html$ https://garrettcounty.us/$2/$1/ [R=301,L,NC]

Path used in Redirect directives: For mod_alias or mod_rewrite you must use path relative to DOCUMENT_ROOT not the full path on filesystem.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The new URIs will take the form /section/date/title-slug, where section can be news, medical, editorial, etc. The date is always YYYYMMDD under the new scheme. Some of the old URLs have titles, some don't. Those were just a few examples I grabbed. I'm very familiar with regular expressions, but there's absolutely no consistency in the old file naming "scheme". I believe a rewrite is out of the question. My primary question is: should I use the absolute directory path or the path from the domain? I'm confused on that part as everything I've found is a bit ambiguous. – Herbert Feb 03 '12 at 07:59
  • For mod_alias or mod_rewrite, you have to use path from DOCUMENT_ROOT not the full path on filesystem. You're right it is impossible to right a rewrite rule with so much inconsistency in naming convention. – anubhava Feb 03 '12 at 08:02
  • 1
    I assume then that the path is relative to DOCUMENT_ROOT for `redirect` as well. That small piece of information was missing from every _how-to_ that I read. Thank you for your help anubhava. – Herbert Feb 03 '12 at 14:09
  • You're welcome, sorry my answer could not sort out your issue though. – anubhava Feb 03 '12 at 14:14
  • 1
    Actually, your comment helped me. The path is indeed relative to DOCUMENT_ROOT. I changed the title to make this easier to find. If you'd be willing to update your answer, I'd be happy to accept it. – Herbert Feb 03 '12 at 14:26
  • 1
    Updated the answer, pls check. – anubhava Feb 03 '12 at 14:30