0

So I've been asked to clean up the URL's for the web app that we're currently running on apache (version 2.4.9 if so interested), but after messing around with mod re_write commands for a few hours, I think I'm running around in circles. Here's what I'm attempting to do.

If the url is something like this (http://www.foo.bar/admin/index.php ), have it display as http://www.foo.bar/admin

If the url is something like this (http://www.foo.bar/admin/testpage.php ), have it display as http://www.foo.bar/admin/testpage

If the url is something like this (http://www.foo.bar/admin/testpage.php?id=15&foo=www ), have it display as http://www.foo.bar/admin/testpage?id=15&foo=www

So far I have it to where the index.php resolves....mostly.....and the second example resolves....sometimes. Basically the code that I have seems very flaky and only resovles two levels down in the file tree. So I was wondering if anyone here could help me out on this at all? here's what I have in the vhost file (because I didn't want to have to have this in every directory as an .htaccess file)

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

1 Answer 1

1

Something like this should work :

RewriteRule ([^/]+)/(.*)/?index.php$ $1
RewriteRule \.php $1

Edit : The first rule could be replaced by this one, which is more straight forward :

RewriteRule (.*)/index\.php?$ $1

e.g :

URL = http://www.foo.bar/admin/index.php

Matched rule = RewriteRule ([^/]+)/(.*)/?index.php$ $1

Result : http://www.foo.bar/admin


URL = http://www.foo.bar/admin/testpage.php

Matched rule = RewriteRule \.php $1

Result = http://www.foo.bar/admin/testpage


URL = http://www.foo.bar/admin/testpage.php?id=15&foo=www

Matched rule = RewriteRule \.php $1

Result = http://www.foo.bar/admin/testpage?id=15&foo=www

2
  • Very nice. The last two seem pretty straight forward, but could you explain the first one? I'm kind of surprised it needs that much compared to the other two's simplicity. May 3, 2015 at 23:08
  • The first one can be "simplified" using this : RewriteRule (.*)/index\.php?$ $1 Seems more readable this way.
    – krisFR
    May 4, 2015 at 6:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .