"Pretty URLs" via htaccess RewriteRules


#1

I set up a pretty non-offensive rewrite rule as a test for a more explicit set of rules conditions that obfuscate actual file names. Turns out that this very very general & very simple version is pretty useful. It looks like this:

RewriteRule ^([a-zA-Z0-9]+)$ $1.php [L]

It basically allows URLs using all the existing files with the file extensions omitted.

I need a version that can work with subdirectories like:
anything/anything rewrites to anything/anything.php

seems simple but I’m having trouble. Any clues would be great.


#2

I found something that looked promising but this also fails to work for files within subdirectories

Paths without file extensions

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?([a-z0-9]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([a-z0-9]+)$ $1.html [NC,L]


#3

The inevitable problem looming, assuming you get your intended outcome to work, is the fatal loss of link juice (or SEO if you prefer that term.)


#4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

#5

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.