Forum Moderators: phranque

Message Too Old, No Replies

URL rewrite

         

beetlethomas

3:37 am on Jul 7, 2010 (gmt 0)

10+ Year Member



Hi there, I have been search for help and reading alot about what I am trying to achieve..

Simply, I need to add a line in htaccess to see if the URL has p40_articleidand replace it just as newsid

How can I achieve this? I have tried

RewriteCond %{REQUEST_URI} ^/p40_articleid/[^/]+$
RewriteRule ^newsid/(.*)$ [L,QSA,R=301]


A full url looks like http://www.example.com/news/p40_articleid/34

But they now need to be http://www.example.com/news/newsid/34

but it dosent seem to work. My current htaccess file looks like

Options All -Indexes
Options +FollowSymLinks
RewriteEngine on

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

RewriteCond %{REQUEST_URI} ^/p40_articleid/[^/]+$
RewriteRule ^newsid/(.*)$ [L,QSA,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

g1smd

6:07 am on Jul 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the
RewriteCond
you specify the request must begin with
p40_articleid
but in the
RewriteRule
you specify the request must begin with
newsid
.

The rule can never run because of that logic error.

Additionally, the real requests are prefixed with "
news/
" so the rule can never run while the ^ "begins with" marker is present, or while
news/
is missing from the pattern.

Redirects should specify the domain name in the target URL. QSA is the default action, so doesn't need to be specified.

You didn't specify a target URL in your redirect.

RewriteRule ^news/p40_articleid/([^/]+)$ http://www.example.com/news/newsid/$1 [R=301,L]


Code assumes "extensionless URLs".

This redirect should also be placed before your general non-www to www redirect. This avoids an unwanted redirection chain for non-www requests.

The pages of your site should be amended to link only to the new URLs.


Finally, the rewrite is too wide in scope. The -f and -d "exists" checks will hammer the server hard drive to death. Add a preceding negative match RewriteCond to exclude filetypes that will not not be rewritten to the script; images, CSS and JS, etc.