homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 37 message thread spans 2 pages: < < 37 ( 1 [2]     
Rewrite .php extension to virtual folder
Rewrite .php extension to virtual folder

 10:45 am on Feb 22, 2010 (gmt 0)


I've been looking and reading for hours and even this post: [webmasterworld.com ], which reads similar to my problem, did not really help me.

So, what I need is a set of rewrite conditions/ rules to rewrite my .php file extensions to a virtual folder at best I'm also forcing a trailing slash.

To make it a little extra complicated: the website is 3 lingual. Each language has it's own directory. Apart from that, there are no other directories.


For this example I will use the english dir:

example.com/english/ contains about 10 .php-files. What I wanna do is to display those .php-files as virtual folders.

example.com/english/index.php -> example.com/english/
example.com/english/news.php -> example.com/english/news/
example.com/english/press.php -> example.com/english/press/

My .htaccess still is quite basic:

Options +Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteBase /

# redirect all non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Everything I've tried so far did not work...I don't know why, as I have some experience with rewriting...but this time, I need your help.

Thanks in advance.



 12:33 am on Feb 23, 2010 (gmt 0)

Done. Same as before.


 12:52 am on Feb 23, 2010 (gmt 0)

Oh,...I did not see jdMorgan's Post. That fixed it! OMG,...I owe you guys. Will subscribe to the forum tomorrow. So much to learn.

Btw. How comes I had tow write this set inline? If the set of rules is not written inline, it leads to a 500 error.

Gotta go to bed now.

Thanks a billion. Really...

[edited by: jabz at 12:55 am (utc) on Feb 23, 2010]


 12:54 am on Feb 23, 2010 (gmt 0)

I mentioned MultiViews a few posts back, but if that's all you missed out of all of this stuff then it was a very good effort!

In the first ( ) of the rewrite pattern, I think (en|de|ar) would be more efficient than ([^/]+). The current pattern is great if there are dozens of languages, but it would be easier to simply list them all when there are way less than ten.

Only three of your four rules have # Comments. Add # Comments to them all.

[edited by: g1smd at 1:01 am (utc) on Feb 23, 2010]


 12:57 am on Feb 23, 2010 (gmt 0)

Like I wrote above... the setup needs to be written inline otherwise I get a 500 error. That's what caused the error, when I tried MultiViews earlier.


# REWRITE url to filepath
RewriteRule ^(en|de|ar)/([^/.]+)$ /$1/$2.php [L]

Works great as well. A little less work for the server...nice tweak.


 1:02 am on Feb 23, 2010 (gmt 0)

Oh, yes. That was bugging me, to mention putting the options all on one line, but I forgot all about it when looking at the rest of the code.

Your final testing will be to request a variety of URLs from the server, mixing www and non-www; with and without trailing slash; index.php and other .php filenames (both valid and non-valid names); as well as non-valid 'language' codes (folders); with and without parameters. That's several dozen combinations.

Check the right content or error message is returned and is visible. Especially use 'Live HTTP Headers' to check the correct 200 / 301 / 404 HTTP response code is returned too.

[edited by: g1smd at 1:08 am (utc) on Feb 23, 2010]


 1:06 am on Feb 23, 2010 (gmt 0)

It works and I'm really impressed and I really appreciate everything you did. The time you took to walk me through this...amazing. Blew my little web developer mind a couple of times. :)

Can't wait to get the book I've ordered, I really need to dig deeper. Apache has so many powerful tools...


 1:12 am on Feb 23, 2010 (gmt 0)

The point of all of this, is that you now understand most of the code, and you should be able to maintain and modify it as your site changes over time.

If I'd simply pasted the answer, you'd have to rely on me to fix stuff years down the line when something went wrong. By adding # Comments there's also a fighting chance it will still make some sort of sense next year. :)

Additionally, this thread is now useful to the next reader battling with the same or similar problems.

This 37 message thread spans 2 pages: < < 37 ( 1 [2]
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