Forum Moderators: phranque

Message Too Old, No Replies

Remove extension and trailing slash

         

mumraa

11:55 am on Jul 30, 2008 (gmt 0)

10+ Year Member



hey there people,

getting annoyed cannot seem to find the answer anywhere, i need to make a URL look like this,

http://example.com/about-us/

at current it is

http://example.com/about-us.php

i need it to rewrite and URL that has an extension and 301 redirect it to the new page,

i did have this, but only seems to work with html and does not add the slash

rewriteCond %{REQUEST_URI} !(\.[^./]+)$
rewriteCond %{REQUEST_FILENAME} !-d
rewriteCond %{REQUEST_FILENAME} !-f
rewriteRule (.*) $1.html [L]

rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP
rewriteRule ^(.+)\.php$ http://example.com/$1/ [R=301,L]

[edited by: encyclo at 1:01 pm (utc) on July 30, 2008]
[edit reason] switched to example.com [/edit]

mumraa

2:42 pm on Aug 1, 2008 (gmt 0)

10+ Year Member



anyone

jdMorgan

9:13 pm on Aug 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll get better responses here with a specific and focused question.

I see four problems:

First, you'll need a second rule to internally rewrite requests to $1.php.

But that creates a second problem, in that you need some way to decide if an extensionless URL should be rewritten to .html or to .php. In order to solve that problem, the requested URLs must differ in some identifiable way -- something that a RewriteRule's regular-expressions pattern can detect.

The third problem is that your external redirect rule will never be invoked, because it requires that the request be for an html page and a php at the same time, which is impossible. The 'filetype' in the RewriteCond and the RewriteRule must be consistent; As it stands, one requires a .html extension, while the other requires a .php extension.

Finally, your rules are in the wrong order. Put your external redirects first, in order from most-specific pattern to least-specific pattern. Follow those with your internal rewrites, again from most-specific to least-specific.

Additionally, though not necessarily a problem, do not feel free to alter case or syntax from exactly what you see in the documentation. mod_rewrite is not a [rogramming language, and does not tolerate 'free-style' coding very well. So, for maximum protability and minimum pain, I suggest your use "RewriteCond" and "RewriteRule" -- exactly as they appear in the documentation.

Jim