Forum Moderators: phranque
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!
*** ...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.
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
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.
*** 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.
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.
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.
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)
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.