...

Simple mod rewrite gone wrong

ubh
04-09-2009, 03:24 PM
Hi I am trying to pass a single variable through mod rewrite to determine what page content should be included into my single document.

So here is what I have.

RewriteRule ^([^/]+).php index.php?pageName=$1 [NC]

If I were to type into the address bar www.mysite.com/home.php it should read the url as

www.mysite.com/index.php?pageName=home

However Its not, what I recive out on the other end is this:

www.mysite.com/index.php?pageName=index

Even though the url is being masked as:

www.mysite.com/home.php

Anyone know whats happening to my variable and why its being read as index instead of home?

abduraooft
04-10-2009, 10:40 AM
RewriteRule ^([^/]+).php index.php?pageName=$1 [NC]
Don't you need to escape that dot(.) before php, like
RewriteRule ^([^/]+)\.php index.php?pageName=$1 [NC]

ubh
04-10-2009, 06:12 PM
abduraooft, thank you for replying.

You are correct I did forget to escape the dot, but this didn't fix it. It still thinks that my variable is always index...

Now that I think about it, I don't think I have ever gotten mod rewrite to ever freaking work under my web servers root folder like this.

If I turn my rewrite into something like this:

RewriteRule ^([^/]+)/index.php index.php?pageName=$1 [NC]

and then put my index.php and .htaccess into a sub folder of the root folder of my server it works, but that's just stupid, I don't want to have to have a sub folder for my home page..

ubh
04-12-2009, 02:46 AM
Ok I know its possible now lol.

I found this site here:
HERE (http://visionmasterdesigns.com/tutorial-creating-pretty-seo-url-using-apache-mod-rewrite/#comment-2431)

And they say this should work.
RewriteRule ^(.*)\.php$ index.php?pageName=$1 [L]

Buts its not. pageName still becomes "index" instead of:

http://localhost/newSite/home.php

which should result in pageName being "home" not "index".

Now I am starting to wonder if its my web server or setup. I know I have mod rewrite installed. Might I have missed something else with my setup?

Apache 2.2 on windows is what I am running.

ubh
04-12-2009, 03:18 AM
Solution
[QSA] Flag instead of [L]

who knew? :p

schleppel
04-15-2009, 09:36 PM
The problem is your rule matches the URL you are rewriting to (/index.php?pageName=home).

You could ignore all existing PHP files
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)\.php$ /index.php?pageName=$1 [QSA,L]

Or ignore sub requests (the request for /index.php?pageName=home)
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /index.php?pageName=$1 [QSA,L]

Or ignore just index.php.
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.+)\.php$ /index.php?pageName=$1 [QSA,L]

destruction
05-26-2009, 08:42 PM
How about hiding the extension file?:confused:

schleppel
05-27-2009, 12:34 AM
Do you want /home to /home.php
Options +FollowSymLinks

RewriteEngine On

# Redirect /file.php to /file/
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

# Rewrite /file/ to /file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]
, /home to /index.php?pageName=home
Options +FollowSymLinks

RewriteEngine On

# Rewrite /page/ to /index.php?pageName=page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?pageName=$1 [QSA,L]
or something else?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum