3

I'm trying to make my urls look prettier in a specific subdirectory on my site by using rewrite to change:
www.example.com/sub/file/{some_number}/ internally to:
www.example.com/sub/file.php?var={some_number}
And I keep getting a 404. I'm already removing the .php extension from my site using an .htaccess file that looks like this:

RewriteEngine on
#
## Internally rewrite extensionless file requests to .php files ##
#
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .php to get the actual filename
RewriteRule (.*) /$1.php [L]
#
## Externally redirect clients directly requesting .html page URIs to extensionless URIs
#
# If client request header contains html file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP
# externally redirect to extensionless URI
RewriteRule ^(.+)\.html$ http://www.dirtycoffee.com/$1 [R=301,L]

# If client request header contains php file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP
# externally redirect to extensionless URI
RewriteRule ^(.+)\.php$ http://www.dirtycoffee.com/$1 [R=301,L]

My current attempt is to add a line at the top like:

#Subdirectory rewrite:
RewriteRule ^sub/file/([0-9]+)/?$ sub/file?num=$1 [L] # Handle requests for file

I know I must have conflicting rules, but I thought [L] would just stop if the new rule was applied.

chris
  • 2,893
  • 3
  • 20
  • 22

1 Answers1

2

Instead of:

RewriteRule ^sub/file/([0-9]+)/?$ sub/file?num=$1 [L]

Try:

RewriteRule ^sub/file/([0-9]+)/?$ /sub/file.php?num=$1 [L]

You're correct that [L] will stop processing rules, but that also means that you can't eliminate the .php extension since the rule for that is below this one. Also note the leading slash.

MVS
  • 1,176
  • 14
  • 22
  • That did it! Thank you so much! I have been staring at this way too long I guess. Also (side-note for Googlers) had to update the links to my css sheets because the page sees it as a directory change. – chris Oct 19 '11 at 20:53