Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Simple mod rewrite gone wrong

    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.

    Code:
    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

    Code:
    www.mysite.com/index.php?pageName=home
    However Its not, what I recive out on the other end is this:

    Code:
    www.mysite.com/index.php?pageName=index
    Even though the url is being masked as:

    Code:
    www.mysite.com/home.php
    Anyone know whats happening to my variable and why its being read as index instead of home?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    RewriteRule ^([^/]+).php index.php?pageName=$1 [NC]
    Don't you need to escape that dot(.) before php, like
    Code:
    RewriteRule ^([^/]+)\.php index.php?pageName=$1 [NC]
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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..

  • #4
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Ok I know its possible now lol.

    I found this site here:
    HERE

    And they say this should work.
    Code:
    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.

  • #5
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    Solution
    [QSA] Flag instead of [L]

    who knew?

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    The problem is your rule matches the URL you are rewriting to (/index.php?pageName=home).

    You could ignore all existing PHP files
    Code:
    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)
    Code:
    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.+)\.php$ /index.php?pageName=$1 [QSA,L]
    Or ignore just index.php.
    Code:
    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
    RewriteRule ^(.+)\.php$ /index.php?pageName=$1 [QSA,L]

  • #7
    New Coder
    Join Date
    Feb 2008
    Posts
    55
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How about hiding the extension file?

  • #8
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    Do you want /home to /home.php
    Code:
    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
    Code:
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •