Forum Moderators: phranque

Message Too Old, No Replies

htaccess mod rewrite issues

301 redirects issue

         

HiHowAreYou

11:18 pm on Mar 1, 2009 (gmt 0)

10+ Year Member



Hi,

I'm in desperate need of help with htaccess and mod_rewrite

I have a site which requires that the file extension in the URLs be removed (e.g - www.example.com/about.php = www.example.com/about , etc)

I have achieved that with the following in htaccess:


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

work perfect

now I have a shop page which needs to rewrite to: www.example.com/catalog/22/12 - where 22 is the category number and 12 is the product number ... I have tried EVERYTHING but keep getting the 500 internal server error - can anybody PLEASE help?
Am I on the right track with the rewrite rule above or do I need to use something different to achieve these two tasks?

Cheers!

g1smd

12:15 am on Mar 2, 2009 (gmt 0)

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



What code did you try?

*** ...needs to rewrite to: www.example.com/catalog/22/12 ***

You have this exactly backwards. The rewrite does not 'make' URLs.

A rewrite would take an external URL request, for a URL like www.example.com/catalog/22/12 - and the rewrite would connect that to an internal filepath, different to that seemingly suggested by the URL.

You need to use the new URL format in all of the links on your pages.

There are several posts with code for rewrites in just the last few days alone.

HiHowAreYou

12:53 am on Mar 2, 2009 (gmt 0)

10+ Year Member



Thanks for the reply g1smd

I have tried:


RewriteRule ^catalog/([0-9]+)/?([0-9]+)/?$ catalog/$1/$2 [L]

AND


RewriteRule ^catalog/([0-9]+)/?([0-9]+)/?$ catalog.php?category=$1&product=$2 [L]

With no success (When I try accessing www.example.com/catalog/22 .. or adding ANYTHING after the catalog URL - I get the internal 500 error)

I guess where im getting confused is where im rewriting the URLs to be extensionless (www.example.com/catalog) and then trying to incorporate query strings into these extensionless URLs.

Thanks for ANY help you can offer.

Thought i'd post my htaccess file for reference:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

DirectoryIndex home.php

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

# Redirect http://www.example.com/home to http://www.example.com
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /home\ HTTP/
RewriteRule ^home\.php$ http://www.example.com/ [R=301,L]

# Hide .htaccess file from public view
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>

# Set 404 error page
ErrorDocument 404 http://www.example.com/404.php

# Remove file extension from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

g1smd

1:08 am on Mar 2, 2009 (gmt 0)

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



You need to put your ''home" redirect before the "www" redirect. Consider what happens now for a non-www request for the home.php file. It hits a double redirect. You get a chain, and that is bad. Reverse the order of just those two.

There should not be question marks in the pattern for your rewrite. Making parts of the URL optional, leads to Duplicate Content issues because multiple URLs resolve to the same content.

HiHowAreYou

1:20 am on Mar 2, 2009 (gmt 0)

10+ Year Member



Ok so changed the order as advised (thanks for that)

Set up following rewrite rule:

RewriteRule ^catalog/([0-9]+)/([0-9]+)$ catalog.php?category=$1&product=$2 [L]

Still 500 internal server error ... even tried:

RewriteRule ^catalog/([0-9]+)/([0-9]+)$ catalog/$1/$2 [L]

g1smd

10:24 am on Mar 2, 2009 (gmt 0)

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



That should work.

RewriteRule ^catalog/([0-9]+)/([0-9]+)$ catalog.php?category=$1&product=$2 [L]

Is it that line that causes the error though?

HiHowAreYou

1:31 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



hmmm ok well i placed that rewrite rule above the rewrite to extensionless URLs ... its display the page now but not any styles/images ... all my links are linking to www.example.com/22/index.php ... its treating the rewritten URL as a directory .. ?

g1smd

1:44 pm on Mar 2, 2009 (gmt 0)

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



We're going to need to tackle your usage of the terminology in order to communicate clearly.

*** the rewrite to extensionless URLs ***

A Rewrite does not *make* a URL. A rewrite changes nothing on the pages of your site. A rewrite changes nothing out there on the web.

A rewrite takes an incoming URL request arriving at your server and silently changes the value for the internal filepath that was hinted at in the URL to be a different internal filepath inside the server, and gets the content from there.

That is, a rewrite takes a URL request for

www.example.com/22/44
and instead of the server looking in
/var/public_html/www/yoursite/22/44
on the hard disk, the rewrite changes the path part to instead get the content from
/var/public_html/www/yoursite/catalog.php?category=22&product=44
without letting anyone back out on the web know that that has happened.

g1smd

1:49 pm on Mar 2, 2009 (gmt 0)

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



*** its display the page now but not any styles/images ***

The location of the images 'on the web' is worked out by the browser, and uses the value of the URL in the browser address bar as a reference point.

So, if the links to images are of the form href="image.jpg" they will be worked out as a relative URL from the folder-part of the current page URL.

So on the page at

www.example.com/22/44
a link to "image.jpg" is saying the image can be found at
www.example.com/22/image.jpg
. That is exactly how HTTP/HTML should work.

To override that behaviour add a leading / and the full path to the image to every image (and stylesheet) link.

That is, change

"image.jpg"
to
"[b]/[/b]image.jpg"
or to
"[b]/images/[/b]image.jpg"
as appropriate.

g1smd

2:02 pm on Mar 2, 2009 (gmt 0)

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



*** A rewrite changes nothing out there on the web. ***

One example.

If anyone requests this URL

example.com/catalog.php?category=22&product=44
they will still be able to access your content.

This is a Duplicate URL for your content.

To prevent this, you will need a 301 redirect from

/catalog.php?category=22&product=44
for both www and non-www requests. The target of the redirect will be
www.example.com/22/44
(i.e. it needs to also force the www within the same redirect) and this redirect needs to be placed before any other redirects listed in your .htaccess file.

HiHowAreYou

10:46 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



ahhh i see now that makes sense :) thanks for clearing it up - just one last thing with the 301 redirects of catalog.php?category=22&product=21 ... whats the best way to write up this 301 to avoid duplicates?

With that rewrite rule, I used the following:

RewriteRule ^catalog/([0-9]+)/?([0-9]+)$ catalog.php?category=$1&product=$2 [L]

I remember you said to not use the ? but when I leave it out and people try to access a category (i.e - www.example.com/22 ) they would get an internal server error - it would only work when they tried to view a product (i.e - www.example.com/22/12 (category = 22, product = 12)

g1smd

11:03 pm on Mar 2, 2009 (gmt 0)

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



*** RewriteRule ^catalog/([0-9]+)/?([0-9]+)$ catalog.php?category=$1&product=$2 [L] ***

What's wrong with using this instead?

RewriteRule ^catalog/([0-9]+)/([0-9]+)$ catalog.php?category=$1&product=$2 [L]
RewriteRule ^catalog/([0-9]+)$ catalog.php?category=$1 [L]

Use two rules. One for category URLs, another for product URLs. The / and the $ ensure only one can match.

.

Alternatively, one rule but with extra logic...

RewriteRule ^catalog/([0-9]+)[b](/[/b]([0-9]+)[b])?$[/b] catalog.php?category=$1&product=[b]$3[/b] [L]

However, the two rules example is more clear.