homepage Welcome to WebmasterWorld Guest from 54.226.192.202
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

    
Using same rules in sub folder
RewriteBase?
Patrick Taylor

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4364687 posted 9:25 am on Sep 20, 2011 (gmt 0)

The .htaccess file in the root folder:

#
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#
# For contact page .php
RewriteRule ^(contact)$ /$1.php [L]
# Rewrite "/string" to "/cms/string.php"
RewriteCond %{THE_REQUEST} ^GET\ /([A-Za-z0-9_\-]+) [NC]
RewriteRule ^([A-Za-z0-9_\-]+)$ /cms/$1.php [L]
# For 'index'
RewriteCond %{THE_REQUEST} ^GET\ / [NC]
RewriteRule ^$ /cms/index.php [L]
# Rewrite "/cms/string.php" to "/string"...
# ...to prevent viewing of 'physical' file
# Use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ /cms/[^.]+\.php\ HTTP/
RewriteRule ^(cms)/([A-Za-z0-9_\-]+)\.php$ /$2 [R=301,L]
#

This seems to work okay. The URLs are suffixless, eg:

/page

I also have the option for the CMS to exist in a sub-folder and want the same behaviour, eg:

/subfolder/page

I cannot get this to work with:

RewriteBase /subfolder

... when the same .htaccess file is in the subfolder. I've had to add /subfolder to the rules, and even then, some of it won't work properly.

Advice would be appreciated. Essentially I want the CMS to be transportable, either installed in the web root or in a sub-folder.

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4364687 posted 9:34 am on Sep 20, 2011 (gmt 0)

The very last rule in your list is commented as a "rewrite". It is a "redirect". Change the comment.

Add a blank line after each RewriteRule to make the code much easier to read.

You must list external redirects before internal rewrites otherwise the redirect will expose rewritten internal filepaths back out on to the web as new URLs.

Be sure to add the standard non-www to www redirect as the last rule of the redirects and before the first rewrite.



A-Za-z simplifies to A-Z when used with the [NC] flag and parses faster.

RewriteRule ^(contact)$ /$1.php [L] simplifies to
RewriteRule ^contact$ /contact.php [L]
There's no need to create the $1 backreference, unless you were doing this:
RewriteRule ^(contact|about|findus|offers)$ /$1.php [L]
Patrick Taylor

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4364687 posted 10:12 am on Sep 20, 2011 (gmt 0)

Understood... thnks.

What I am trying to do though, is use the rules that work in the root when the CMS is installed in a sub-folder. When I add:

RewriteBase /subfolder

... to that same .htaccess file (in the subfolder) and visit the subfolder where the CMS is installed, it still puts me back in the root and I'm just seeing /subfolder in the URL. The RewriteBase seems to have no effect.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4364687 posted 12:09 pm on Sep 20, 2011 (gmt 0)

"RewriteBase /subfolder" maps an incoming root URL request to an internal folder path in the /subfolder folder without revealing the content will be served from a folder.

So, the FILES will be installed in a folder, but do you want /folder/ to appear in the URL for the CMS or not? I assume not. That's the whole point of RewriteBase; request a root URL and have it silently served from a folder.

The rules need to sorted into the correct order, otherwise there will be unwelcome interactions between the various rules.

The RewriteBase directive will ideally appear after the redirects and before the rewrites.

Patrick Taylor

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4364687 posted 12:42 pm on Sep 20, 2011 (gmt 0)

I want the /folder/ to appear in the URL when the CMS in /folder/ is being used, but not when the CMS in the root is being used. It is supposed to be installable in either (or both simulaneously) with the correct paths to each.

I'd misunderstood RewriteBase /folder. I was thinking it should be written into the /folder/ .htaccess (then the rest of .htaccess could be the same for both) but from what you say, the rule goes in the root .htaccess - but that isn't what I'm looking for anyway. I can now see I need specific .htaccess files for the root and for the subfolder.

Thanks for the explanation.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4364687 posted 12:54 pm on Sep 20, 2011 (gmt 0)

You need specific *rules* for root URLs and for folder URLs, not specific htaccess *files* for root and for folder.

The root htaccess file is processed first, so unless the rules in there are "aware" of the folder CMS all of the incoming URL requests will be served by the root CMS.

Ideally all of the rules for both CMS versions will go in the root htaccess file.

# Rewrite for folder CMS
RewriteBase /folder-CMS
RewriteRule ^subfolder/(.*) /index.php [L]


# Rewrite for root CMS
RewriteBase /
RewriteCond $1 !^subfolder
RewriteRule (.*) /index.php [L]


Before you write *any* code you need to work out what URL requests for categories, pages, images, stylesheets, javascript files, etc "look like" (and do it for both root and folder), because each of your rules will need to include or exclude the handling of those.

Proceeding without working that out will likely mean your next post is "I have got my CMS sort of working but the pages are unstyled and all the images are missing". It's a common second question in any thread about URL rewriting.

[edited by: g1smd at 1:47 pm (utc) on Sep 20, 2011]

Patrick Taylor

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4364687 posted 1:44 pm on Sep 20, 2011 (gmt 0)

Well... everything works okay now in either location, thanks. Alternative versions of the .htaccess file according to whether in root or /folder/. I've not had problems with stylesheets, images, etc - the CMS config file takes care of that.

I should explain that when the CMS is installed in /folder/ it has to be independent of the root and whatever .htaccess file is in the root. Independent of the root in all respects.

Patrick Taylor

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4364687 posted 3:11 pm on Sep 20, 2011 (gmt 0)

User comments out as required, depending on whether CMS is in root or /folder/:

###
AddDefaultCharset UTF-8

Options +FollowSymLinks
RewriteEngine on

### WHEN USED IN "/" ROOT FOLDER:

### Redirect "/cms/string.php" to "/string"
RewriteCond %{THE_REQUEST} ^GET\ /cms/[^.]+\.php\ HTTP/
RewriteRule ^cms/([A-Za-z0-9_\-]+)\.php$ /$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/cms/inc/list\.php$ [OR]
RewriteCond %{REQUEST_URI} ^/cms/inc/menu\.php$
RewriteRule .* - [F]

RewriteRule ^contact$ /contact.php [L]

RewriteCond %{THE_REQUEST} ^GET\ /([A-Za-z0-9_\-]+)\ HTTP/
RewriteRule ^([A-Za-z0-9_\-]+)$ /cms/$1.php [L]

RewriteCond %{THE_REQUEST} ^GET\ /\ HTTP/
RewriteRule ^$ /cms/index.php [L]

### WHEN USED IN "/folder/" SUB FOLDER:

### Redirect "/folder/cms/string.php" to "/folder/string"
# RewriteCond %{THE_REQUEST} ^GET\ /folder/cms/([A-Za-z0-9_\-]+)\.php\ HTTP/
# RewriteRule ^folder/cms/([A-Za-z0-9_\-]+)\.php$ /folder/$1 [R=301,L]

# RewriteCond %{REQUEST_URI} ^/folder/cms/inc/list\.php$ [OR]
# RewriteCond %{REQUEST_URI} ^/folder/cms/inc/menu\.php$
# RewriteRule .* - [F]

# RewriteRule ^contact$ /folder/contact.php [L]

# RewriteCond %{THE_REQUEST} ^GET\ /folder/([A-Za-z0-9_\-]+)\ HTTP/
# RewriteRule ^([A-Za-z0-9_\-]+)$ /folder/cms/$1.php [L]

# RewriteCond %{THE_REQUEST} ^GET\ /folder/\ HTTP/
# RewriteRule ^$ /folder/cms/index.php [L]
###

Users may have their own arrangements for error404 and canonical so they are left out.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4364687 posted 3:20 pm on Sep 20, 2011 (gmt 0)

RewriteCond %{REQUEST_URI} ^/cms/inc/list\.php$ [OR]
RewriteCond %{REQUEST_URI} ^/cms/inc/menu\.php$


simplifies to

RewriteCond %{REQUEST_URI} ^/cms/inc/(menu|list)\.php$ and parses faster.


[A-Za-z0-9_\-] simplifies to [a-z0-9_-] [NC] and parses faster.
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