views:

43

answers:

1

I need a little help with some mod_rewrite rules I am trying to use. I need to do 2 things

  1. Change a dynamic URL so that it’s friendly (e.g. /content.php?CategoryID=1 change to /categories/1/)

  2. Remove the .php extension on my pages (e.g. /page2.php to /page2)

Ok so I can get number 1 to work by itself with the following in my .htaccess file:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteOptions Inherit
RewriteBase /

RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1

I am then trying number 2 and I'm running into a few problems. I am using the following:

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]

The above rerwrites the URL so it looks friendly (e.g. /page2/) but it hits my 404 page and I can't view it but my other page where I rewrite the URL still works (e.g. /category/1/).

This is my full .htaccess file:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteOptions Inherit
RewriteBase /

RewriteRule ^category/([0-9]+)?$ category/$1/ [R]
RewriteRule ^category/([0-9]+)/?$ content.php?CategoryID=$1

RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]

ErrorDocument 404 /misc/404page.html

Any help would be greatly appreciated as I am new to all this mod_rewrite and regex stuff.

A: 

I know it's been a while, and I'm not 100% sure what you were trying to do, but I would probably be looking at:

RewriteRule ^category/([0-9]+)$ category/$1/ [R,L]
RewriteRule ^category/([0-9]+)/$ content.php?CategoryID=$1 [L]
RewriteRule \.php? - [L]
RewriteRule ^(.+)\.php$ $1 [R,L]
RewriteRule ^([^/?])$ $1.php [L]

I'd need to look at your .htaccess more closely and/or have a better idea of the structure of your site to give better information.

Andrew