Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite case

why not work in subdir?

         

ThA WebMasteR

2:42 pm on Jun 16, 2004 (gmt 0)

10+ Year Member



I am using the following .htaccess to rewrite urls:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/redirect/
RewriteRule ^[^/]+/(.*) /index.php?$1 [R,L]

When I put this in the root directory of my domain everything works fine. However, when I place it in a subdir, then it fails. What am I doing wrong? Or is it impossible what I want?

thanks in advance.

ThA WebMasteR

jdMorgan

2:18 pm on Jun 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ThA WebMasteR,

Welcome to WebmasterWorld [webmasterworld.com]!

For each level you go down in placing your .htaccess code, you will need to remove one directory match from your RewriteRule pattern; The URL-path 'seen' by RewriteRule is relative to the directory in which it is executed.

So, for example, if you put the .htaccess code one subdirectory down, you'll need to remove the leftmost [^/]+/ in the RewriteRule pattern:


RewriteRule (.*) /index.php?$1 [R,L]

Note also that you'll need to take care to avoid redirect recursion (looping), especially since you're doing an external redirect here, so you'd likely need:

RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule (.*) /index.php?$1 [R,L]

JIm

ThA WebMasteR

6:00 pm on Jun 17, 2004 (gmt 0)

10+ Year Member



Thanks Jim for your reply,

I am developing a CMS for myself, but I want to use it also on sites which are in a folder of the domain. Like [yourdomain.com...] When a user goes to [yourdomain.com...] the .htaccess is forced to open: [yourdomain.com...] . Do you get what I want?
The .htaccess I wrote only works when the cms is in: [yourdomain.com...] Or is it impossible what I want?

Thanks for your reply in advance!

ThA WebmasteR

jdMorgan

2:17 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want the php script to be referenced in a fixed location, but simply substitute it for the requested URL, then you should begin your substitution with "/" as you have done, and then provide the entire URL-path from server root to the script. If you use an external redirect, then you should provide a canonical URL i.e. http://yourdomain.com/folder/folder/cms/index.php as the path.

If you want the script to be refernced using a relative path (relative to the directory where this .htaccess code resides), then omit the leading slash.

The key here is to know *where* the script resides, and to provide a complete or relative path to it.

Assuming the .htaccess code goes into folder "folder1"
redirecting http://yourdomain.com/folder1/folder2/cms/redirect/home to http://yourdomain.com/folder1/folder2/cms/index.php?home
your choices would be:

# "Silent" internal rewrite (browser shows original URL) using fixed local script location
RewriteRule ^folder2/cms/redirect/(.+) /folder1/folder2/cms/index.php?$1 [L]
#
# "Silent" internal rewrite (browser shows original URL) using relative local script location
RewriteRule ^folder2/cms/redirect/(.+) folder2/cms/index.php?$1 [L]
#
# External redirect (new URL shows in browser) using canonical URL (script can be on any server)
RewriteRule ^folder2/cms/redirect/(.*) http://www.yourdomain.com/folder1/folder2/cms/index.php?$1 [R=301,L]

Note that because the .htaccess code resides in /folder1, that part of the URL is stripped, and is not used in the RewriteRule pattern-matching.

Refs:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]

Jim

ThA WebMasteR

11:16 am on Jun 18, 2004 (gmt 0)

10+ Year Member



Thanks Jim for your help. I now use the following .htaccess:

php_flag register_globals on
Options +FollowSymLinks
RewriteEngine on
RewriteBase /dev/

RewriteCond %{REQUEST_URI} !^(.+)index\.php
RewriteCond %{REQUEST_URI} !^(.+)components
RewriteCond %{REQUEST_URI} !^(.+)data
RewriteCond %{REQUEST_URI} !^(.+)images
RewriteCond %{REQUEST_URI} !^(.+)plugins
RewriteCond %{REQUEST_URI} !^(.+)templates
RewriteRule ^(.*) index.php?$1 [L]

in my cms I use encrypted URL's to go to pages. When I wnt to go to: [localhost...]
NVEkzTGpBdU1DNHh8Ym1WNGRIQnBaQT09fFgyaHZiV1U9fGFXNXNiMmRmYldWemMyRm5aUT09fFZTQnJkVzUwSUdocFpYSnZibVJ
sY2lCVmR5Qm5aV0p5ZFd0bGNuTnVZV0Z0SUdWdUlIZGhZMmgwZDI5dmNtUWdhVzRnZEdVZ2RuVnNiR1Z1TENCdmJTQnBiaUIwWlN
Cc2IyZG5aVzRnYjNBZ1pHVjZaU0J6YVhSbElRPT0=b1512

The server fails to load te page. However, if I use the following url to go to the SAME page, it doesn't fail:
[localhost...]
nh2WjJsdXxNVEkzTGpBdU1DNHh8Ym1WNGRIQnBaQT09fFgyaHZiV1U9fGFXNXNiMmRmYldWemMyRm5aUT09fFZTQnJkVzUwSUdoc
FpYSnZibVJsY2lCVmR5Qm5aV0p5ZFd0bGNuTnVZV0Z0SUdWdUlIZGhZMmgwZDI5dmNtUWdhVzRnZEdVZ2RuVnNiR1Z1TENCdmJTQ
nBiaUIwWlNCc2IyZG5aVzRnYjNBZ1pHVjZaU0J6YVhSbElRPT0=b1512

Failing in this case means a 403 Forbidden message. Could it be the problem that the url is to long?

Thanks!

[edited by: jdMorgan at 2:49 pm (utc) on June 18, 2004]
[edit reason] wrapped long lines to prevent side-scroll [/edit]

jdMorgan

2:58 pm on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those URLs are certainly longer than I'd care to use, but the difference might be that the first request invokes your rewrite, and the second request won't -- since your first RewriteCond will prevent a rewrite on any URL containing "index.php", which it does.

However, as I previously mentioned, you've not provided a fixed path to index.php in that RewriteRule, and that is the first thing I would experiment with in this case. Try something like this:


RewriteCond %{REQUEST_URI} !^.+(index\.php¦components¦data¦images¦plugins¦templates)
RewriteRule ^(.*) [b]/dev/[/b]index.php?$1 [L]

(Change the broken pipe "¦" characters to solid pipes before use -- posting here modifies them.)

Jim