homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Htaccess removing extension problem

 9:44 pm on Feb 23, 2011 (gmt 0)

This is part of my htaccess file:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Using this I can remove .php from all my URL's so www.example.com/index.php now works as www.example.com/index which is great. I have updated my linking schema on the site so I now use the URL's with the extension removed.

This works great for 80% of pages except when I have to pass a variable into the URL such as www.example.com/edit_user.php?id=5. If I try www.example.com/edit_user?id=5 it gives a 404 error so how do I deal with this? At the moment I am having to use the full URL which I would rather not do as it is inconsistent and ideally I would like to be able to use something like www.example.com/edituser/id/5 - is that possible?

Many thanks




 11:39 pm on Feb 23, 2011 (gmt 0)


No need for "id" to be in the URL.

Rewrite requests for
www.example.com/edituser/5 to internal server path at /edit_user.php?id=5

RewriteRule ^edituser/([0-9]+) /edit_user.php?id=$1 [L]

Do not use
(.*) in the pattern. Use a more specific pattern so that only specific URL formats can be rewritten.

 6:47 pm on Feb 24, 2011 (gmt 0)

Thank you for this - how would the code change if the file was in a sub directory? Do I just change the path in the code or do I modify the Rewrite base?

For example if edit_user.php was in www.example.com/users/ how would I modify this?




 7:54 pm on Feb 24, 2011 (gmt 0)

The rewrite target is the real location inside the server filesystem where the content resides, so:

RewriteRule ^edituser/([0-9]+) /users/edit_user.php?id=$1 [L]

Remember, a rewrite does NOT "make URLs for your content". Instead, the server accepts a URL request and if the path part of the URL matches the RegEx pattern, then the rewrite fires to fetch the content from somewhere inside the server.

In plain English, the left side attempts to match the requested URL and the right side is the internal server location where the content really resides.


 12:52 pm on Feb 25, 2011 (gmt 0)

Got you now - so instead of doing this is my root htaccess file I add this to the htaccess in the folder in question so the left part is always local to that file and the right part is the whole path?

I assume in this case whatever is in the web root htaccess still applies even if I put a new one in a sub directory? i.e. the new file in the subdirectory complements the web root one rather than replaces it?


 1:39 pm on Feb 25, 2011 (gmt 0)

Yes, the root .htaccess rules are processed first.

Yes, the path part of the pattern is "localised" to the current folder.

If you scatter the functionality of your site across several .htaccess files you might run into problems with rules being processed in the wrong order.

Most problematical is a rewrite in the root combined with a redirect in a folder. The redirect will re-expose previously rewritten server internal filepaths back out on to the web as new URLs.


 4:24 pm on Feb 27, 2011 (gmt 0)

Hmmmmm can get the URL redirect to work on its own so thanks for that but cant get it to work in conjunction with the other htaccess!

Web root htaccess contains:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} app
RewriteRule ^(.*)$ https://www.example.com/app/$1 [R,L]
RewriteEngine on
RewriteBase /app/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
ReDirect 301 /app/user/index.php http://www.example.com/app/user/user

This works fine - all pages redirect to https, extension is removed and when anyone tries /app/user or /app/user/index.php it redirects to /app/user/user.

If I add this line "RewriteRule ^edituser/([0-9]+) /users/edit_user.php?id=$1 [L]" into the root htaccess it gives me 500 Internal Server Error.

If I put it into the /app/user directory I can get the url to work i.e. /app/user/user-edit/5 allows me to edit user 5 BUT then the app/user/index.php redirect stops working.

Assume I need to get all the redirects for that directory into the one htaccess file located there?

Have tried all variations I can think of including variations of RewriteBase and changing the paths in the actual ReWriteRule but cant get them to co-exist, can they be made to work in conjunction with each other?




 5:03 pm on Feb 27, 2011 (gmt 0)

The line with Redirect is a problem. You cannot mix Redirect and RewriteRule syntax anywhere within the same site.

Use RewriteRule for all of the rules.

The R flag gives a 302 redirect. You probably need R=301,L

Add the L flag to every RewriteRule.

The RewriteEngine On directive is needed only once.
Add a blank line after each RewriteRule to make the code readable.

Add a comment before each block of code to describe what it does.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved