User specified target path

Discuss practical ways rearrange URLs using mod_rewrite.

User specified target path

Postby axtg » Tue Mar 10, 2009 3:54 pm

Hi!

Please note: I've read through the FAQ and in particular "Relative paths to images...", but my question seems slightly, if not completely, different.

Having said that :)...

For a CMS I have developed I use a .htaccess file to rewrite urls like w*w.site.com/home.html to w*w.site.com/index.ph?page=home. Which works perfectly using the lines below:

Code: Select all
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /index.php?page=$1 [L]


However I've noticed that users of my CMS often want to install the CMS in a deeper folder (www.site.com/cms/home.html). Which makes the lines above non-functional.

I've tried removing the '/' before index.php, but this makes the server look in a folder above the public html folder. I've tried './', but that apparently only works in PHP. I've tried system variables (e.g. %{DOCUMENT_ROOT}, but no luck.

So my question is whether this is actually an option. That the index.php file is looked for in the same directory the .htaccess file resides. Sounds do-able, but I haven't figured it out...

Thanks!
axtg
 
Posts: 6
Joined: Tue Mar 10, 2009 11:37 am

Postby richardk » Wed Mar 11, 2009 10:31 am

That the index.php file is looked for in the same directory the .htaccess file resides.

./ should do that.

I've tried removing the '/' before index.php, but this makes the server look in a folder above the public html folder.

Which directory? Does it have any significance to your setup?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby axtg » Sat Mar 21, 2009 8:15 am

Hi Richard,

Thanks for your reply. I'll give you some details to make it more clear what the structure is like and what goes wrong for some people.

* Using Apache on Windows (WAMP) I have set-up a test site in the directory: I:\Server\www\dev\testsite\
(where www is the root; this config would be available under localhost/dev/testsite).

* In the root of the project (/dev/testsite/) resides a .htaccess file with a line similar to the one below:

Code: Select all
RewriteRule ^includes/js/(.*\.js) ./includes/combine.inc.php?type=javascript&files=$1


Hence expecting that .htaccess would redirect any *.js requests to localhost/dev/testsite/includes/combine.inc.php. However: when I check Firebug it shows a 404 error saying:

The requested URL /Server/www/dev/testsite/includes/combine.inc.php was not found on this server.

--
Apparently I'm missing out on some setting which causes the .htaccess file to look in the complete site root. I've tried setting the RewriteBase to both / and ./, but no luck with that either.

Thanks for your time!
Xander.
axtg
 
Posts: 6
Joined: Tue Mar 10, 2009 11:37 am

Postby richardk » Sat Mar 21, 2009 2:20 pm

The requested URL /Server/www/dev/testsite/includes/combine.inc.php was not found on this server.

You must have a redirect somewhere. Do you have some other rules?

Try
Code: Select all
RewriteRule ^includes/js/(.+\.js)$ ./includes/combine.inc.php?type=javascript&files=$1 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby axtg » Sun Mar 22, 2009 4:16 am

Thanks again for your time, but unfortunately no success yet.

While I have more code in the .htaccess file, I've removed non relevant ones for debugging reasons; so currently there isn't anything else than the three lines below in the .htaccess file.

Code: Select all
RewriteEngine On

RewriteRule ^includes/js/(.+\.js)$ ./includes/combine.inc.php?type=javascript&files=$1 [L]
RewriteRule ^css/(.+\.css)$ ./includes/combine.inc.php?type=css&files=$1 [L]


Trying the above still gives an 404 error telling me that the combine.inc.php file couldn't be found in /Server/www/dev/testsite/includes/ (which still is the complete path).

Other thoughts, if any, are still much appreciated :)!

Thanks again,
Xander.

P.s.: for a minute there I thought it could have something to do with the Virtual Alias I have set-up for the /dev/ directory, but removing it didn't help.
axtg
 
Posts: 6
Joined: Tue Mar 10, 2009 11:37 am

Postby richardk » Sun Mar 22, 2009 11:09 am

Try replacing ./ with just /.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby axtg » Sun Mar 22, 2009 12:06 pm

I might have understood you wrong here, since I couldn't think of anything else what you might have mend with this. So I tried to set it to:

Code: Select all
RewriteEngine On

RewriteRule ^includes/js/(.+\.js)$ /.
RewriteRule ^css/(.+\.css)$ /.


Resulting in showing (in Firebug) the source of the index.php file in the root of the server (I:\Server\www\index.php). Hope that helps in any way!

Xander.
axtg
 
Posts: 6
Joined: Tue Mar 10, 2009 11:37 am

Postby richardk » Tue Mar 24, 2009 2:30 pm

I meant (i did not explain it well):
Try replacing
Code: Select all
./

with just
Code: Select all
/
.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^includes/js/(.+\.js)$ /includes/combine.inc.php?type=javascript&files=$1 [L]
RewriteRule ^css/(.+\.css)$        /includes/combine.inc.php?type=css&files=$1        [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby axtg » Tue Mar 24, 2009 2:37 pm

Which would work fine if the .htaccess would be in the root directory (directly under www), but now when in a subdirectory.

E.g. a page located on http://localhost/dev/testsite/ would look in http://localhost/includes/combine.inc.php instead of http://localhost/dev/testsite/includes/combine.inc.php with this .htaccess configuration. I know I should set it to /dev/testsite/includes/combine.inc.php in .htaccess if I wanted to achieve this, but I was wondering if there is an option for a relative path, because the location changes now and then.

Thanks for another effort!
Xander.
axtg
 
Posts: 6
Joined: Tue Mar 10, 2009 11:37 am

Postby richardk » Tue Mar 24, 2009 2:53 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteBase /dev/testsite/

RewriteRule ^includes/js/(.+\.js)$ includes/combine.inc.php?type=javascript&files=$1 [L]
RewriteRule ^css/(.+\.css)$        includes/combine.inc.php?type=css&files=$1        [L]


Edit: I suggest you look at <VirtualHost>s and adding "domains" to your hosts file instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron