0

Another edition to my .htaccess rewriting saga. Everything is now working to a degree but still have some issues.

I have numerous rules to move old urls to new urls, passing variables etc - however there are still a couple of things i need to add, and for the life of me cannot figure out.

I have 3 urls that are directing/rewriting as below.


1 www.mydomain.com/news/dentistry_dental/index.php

2 www.mydomain.com/news/dentistry_dental/index.php?month=April&year=2011

3 www.mydomain.com/news/dentistry_dental/article_detail.php?article=1234&title=some-title


These are redirected & rewritten perfectly to the new urls respectively


1 www.mydomain.com/dental_news/

2 www.mydomain.com/dental_news/April-2011

3 www.mydomain.com/dental_news/1234-some-title


However... Here is the problem #1 The following urls are also redirecting as below


4 www.mydomain.com/news/it_technology/index.php?month=April&year=2011

5 www.mydomain.com/news/it_technology/article_detail.php?article=1234&title=some-title


Which are also directing to the same urls as the dental redirects


4 www.mydomain.com/dental_news/April-2011

5 www.mydomain.com/dental_news/1234-some-title


Which shouldnt be happening. The it_technology news articles have now been removed so i wish to redirect them either to my homepage with a 410 or something similar, whichever is the best option really.

My current .htaccess looks as follows.

Options +FollowSymlinks RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

    # Rewrite all index.php to root: / ( with perm redirect )
    RewriteCond %{REQUEST_URI} !/generator/
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

    RewriteRule ^products/dental-digital-imaging/([^/]*)_([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]
    RewriteRule ^products/package_deals.php$ http://www.mydomain.com/products/dental-computer-network-bundles [R=301]
    RewriteRule ^products/computer_hardware.php$ http://www.mydomain.com/products/dental-computer-solutions [R=301]
    RewriteRule ^products/individual_computers.php$ http://www.mydomain.com/products/dental-computer-systems [R=301]
    RewriteRule ^products/digital_xray_imaging.php$ http://www.mydomain.com/products/dental-digital-imaging [R=301]

    RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]

    # Rewrite dental news article to neat nice url
    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule ^dental_news/([0-9]*)-([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]

    #Conditional rewrite of old news article path to new one with 301 redirect
    RewriteCond %{REQUEST_URI} !^/dental_news/
    RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
    RewriteRule (.*) /dental_news/%1-%2? [L,R=301]

    RewriteCond %{REQUEST_URI} !^/dental_news/
    RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
    RewriteRule (.*) /dental_news/%1-%2? [R=301]

    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]

    # Protect from looping because of previous rules
    RewriteCond %{QUERY_STRING} !rewrite
    RewriteRule ^dental_news/([a-zA-Z]*)-([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]

    # Rewrite URL stripping .php Extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

My only other requirement would be to add a trailing slash to the php files served ( that have allready add the .php extension removed with the last rule in my .htaccess.

I have tried numerous ways that i have found via google, but all of them cuase probelms with my other rules.

Hopefully someone can help me finish this off once and for all.

Regards M

2 Answers 2

1

Sure is getting complicated :) Try changing these lines (I think there are 2):

RewriteCond %{REQUEST_URI} !^/dental_news/

to:

RewriteCond %{REQUEST_URI} ^/news/dentistry_dental/

That way its only going to redirect the old dental news URLs to there respective new ones.

2
  • Hi clmarquart, thanks again for your input, i shall try this option also and report back shortly, many thanks again for your help. Regards M
    – madmatuk
    Apr 14, 2011 at 14:16
  • Worked perfectly - i have redirected my old it news pages also, so thats all sorted, only the trailing slashes to sort now :)
    – madmatuk
    Apr 14, 2011 at 15:47
0

Trivially, your problem is the catch-all rules:

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1-%2? [R=301]

Restrict them to the path you want to rewrite, i.e. replace .* with something more specific, like:

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule ^news/dentistry_dental/index.php$ /dental_news/%1-%2? [R=301]

You can do the news/it_technology redirect as a standard redirect.

RewriteRule ^news/it_technology http://www.thepage.com/ [R=301,L]

It might technically be more correct to do a 410, but the 410 code is really optional. You might also want to make it 404, and then have a link from the 404 page to the main page.

2
  • Ahhh, thank you very much, i will try what you have suggested and report back shortly. Many thanks for your time. Regards M
    – madmatuk
    Apr 14, 2011 at 14:14
  • The changing of the existing rules worked, however the 2nd rewrite for the it_Techonolgy didnt, i have sorted this now and used the solution below. Just need to sort my trailing slashes now. :)
    – madmatuk
    Apr 14, 2011 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.