Why doesn't this mod_rewrite rule work?

New to mod_rewrite? This is a good place to start.

Why doesn't this mod_rewrite rule work?

Postby carlos123 » Wed Sep 16, 2009 6:20 pm

Hi everyone,

I have been struggling to make some mod_rewrite rules work for days and while I have made great progress I still can't get some to work properly.

Specifically the following is one that doesn't work...

# Doesn't work with "http://domain.local/about-me"
RewriteRule ^/about-me$ index.php?content=about-me [E=RULE:4,L]

I don't understand why not?

It should as far as I can see. I mean that it should create the environment variable RULE and set it to equal 4 but no such variable ends up being created. Removing the "/" in front of about-me in the regular expression portion doesn't help.

I am using Apache running on localhost (thus the .local domain ending).

I would appreciate any insight anyone could give me.

Thanks.

Carlos
carlos123
 
Posts: 3
Joined: Tue Sep 15, 2009 9:35 pm

Postby richardk » Thu Sep 17, 2009 9:37 am

Do you have some other working rules? Could you post the whole mod_rewrite.
Where are you putting the mod_rewrite?
Do you have a file or directory called about-me or about-me.something?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby carlos123 » Thu Sep 17, 2009 9:48 am

Hi Richard,

Thanks for your input.

Below is my .htaccess file. It is located in my document root (i.e. the base directory of my web site). Lessons is a subdirectory off my document root but for the life of me I can't get the .htaccess file inside the document root to process url's with the directory name in them.

Maybe I have to have a seperate .htaccess file inside the lessons directory. Don't know. I guess I have just been assuming that the .htaccess found in the document root is applicable to all subdirectories too. At least that is what the Apache documentation says.

Code: Select all
RewriteEngine on

# Doesn't work with "http://domain.local/lessons/about-me"
RewriteRule  /lessons/about-me$     index.php?content=about-me [E=RULE:1,L]

# Doesn't work with "http://domain.local/lessons/about-me"
RewriteRule  ^lessons/about-me$      index.php?content=about-me [E=RULE:2,L]

# Works with "http://domain.local/about-me"
RewriteRule  ^about-me$              index.php?content=about-me [E=RULE:3,L]

# Doesn't work with "http://domain.local/about-me"
RewriteRule  ^/about-me$             index.php?content=about-me [E=RULE:4,L]

# Works...SPECIAL_PATH found in PHP $_SERVER array.
#SetEnv SPECIAL_PATH /foo/bin

# Works...locally_linked found in PHP $_SERVER array.
#SetEnvIfNoCase Referer "^$" locally_linked=1

ErrorDocument 403 /index.php?id=page-not-viewable
ErrorDocument 404 /index.php?id=page-not-viewable
ErrorDocument 405 /index.php?id=page-not-viewable
ErrorDocument 500 /index.php?id=page-not-viewable


The only one that works is the one that creates the env RULE equal to 3.

In other words if I type "http://domain.local/about-me" into the address bar of my browser only that line works. Now obviously the first two RewriteRules wouldn't work with that line either but when I type their particular URL's into the browser they don't work either.

Any insight on this Richard would be appreciated. I have spent hours trying to figure this out and have hit a solid wall.

My goal is to create an env variable that will allow my PHP scripts to determine what the navigation links showing up on page are so that global pages (which reside in my root base directory) called from any page on my site will not result in the home set of navigation links showing up but in the local links that are showing up on the page where the link was clicked.

Carlos
carlos123
 
Posts: 3
Joined: Tue Sep 15, 2009 9:35 pm

Postby richardk » Thu Sep 17, 2009 10:07 am

In .htaccess files (and <Directory>s) the part of the URL the RewriteRule matches against is relative to the directory the .htaccess file is in. This means that the pattern should not start ^/ in your document root.

I guess I have just been assuming that the .htaccess found in the document root is applicable to all subdirectories too. At least that is what the Apache documentation says.

You are correct, it should apply to all sub directories, unless there is a .htaccess file in that sub directory with mod_rewrite in it.

Try
Code: Select all
ErrorDocument 403 /index.php?id=page-not-viewable
ErrorDocument 404 /index.php?id=page-not-viewable
ErrorDocument 405 /index.php?id=page-not-viewable
ErrorDocument 500 /index.php?id=page-not-viewable

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^lessons/about-me$ /index.php?content=about-me [E=RULE:2,L]
RewriteRule ^about-me$         /index.php?content=about-me [E=RULE:3,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby carlos123 » Thu Sep 17, 2009 10:46 am

Hi Richard,

The RULE = 3 line does work but the RULE = 2 line does not (even when I remove the leading "/").

Carlos
carlos123
 
Posts: 3
Joined: Tue Sep 15, 2009 9:35 pm

Postby richardk » Thu Sep 17, 2009 3:52 pm

What happens if you add the R (Redirect) flag?

Add a RewriteLog to your server configuration (httpd.conf file) or, if you are using one, your <VirtualHost>.
Code: Select all
RewriteLogLevel 9
RewriteLog "/path/to/rewrite.log"

Then go to /lessons/about-me and post the log here.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 31 guests

cron