[SOLVED] mod_rewrite noob problem.

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

My site is setup with an index.php which everything goes through. So index.php?page=news, but it looks bad so I'm doing this...
Code: [ Select ]
 
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)$ index.php?page=$1
  1.  
  2. RewriteEngine on
  3. RewriteRule ^([a-zA-Z0-9_]+)$ index.php?page=$1


Which works with http://mysite.com/news, but it doesn't work with http://mysite.com/news/, so I tried this thing I found in a tutorial...

Code: [ Select ]
 
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$ /index.php?page=$1
  1.  
  2. RewriteEngine on
  3. RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
  4. RewriteRule ^([a-zA-Z0-9_]+)/$ /index.php?page=$1


But it totally doesn't work either way now and it says it's not found on the server?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Prime
  • Professor
  • Professor
  • User avatar
  • Posts: 935
  • Loc: Liverpool

Post 3+ Months Ago

# RewriteEngine on
# RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
# RewriteRule ^([a-zA-Z0-9_]+)/$/ /index.php?page=$1

I haven't tested this but give it a try, subtle difference.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Oh thanks, sorry for late reply I was having e-mail problems. It doesn't seem to work though, I just plugged it in though I'm kinda spaced out though atm, so it might be me lol.

When I type blahblahblah.com/news
The requested URL /news was not found on this server.

When I type blahblahblah.com/news/
The requested URL /news/ was not found on this server.

This is what I have, this bad?
Code: [ Select ]
 
Options +FollowSymLinks
 
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$/ /index.php?page=$1
 
  1.  
  2. Options +FollowSymLinks
  3.  
  4. RewriteEngine On
  5. RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
  6. RewriteRule ^([a-zA-Z0-9_]+)/$/ /index.php?page=$1
  7.  
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

I sort of figured it out.

Code: [ Select ]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [QSA,L]
  1. RewriteCond %{SCRIPT_FILENAME} !-f
  2. RewriteCond %{SCRIPT_FILENAME} !-d
  3. RewriteRule ^([^/.]+)/?$ index.php?page=$1 [QSA,L]


It does it's job, the problem is if they type in mywebsite.com/news/ the browser thinks news is another directory, when I try to include any other pages or link any CSS or javascript it looks into a news/ directory which doesn't exist?

Like normally on my page it includes a CSS file with this path: themes/css.php
So the browser goes to: mysite.com/themes/css.php

But if the mod re-write has a trailing slash it will send mysite.com/news/ to the browser, so the browser thinks it's a directory and themes/css.php is considered mysite.com/news/themes/css.php which obviously doesn't exist, anybody know how to fix this problem?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

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

Try that but I only read a guide some time ago and not very good at it so I may get it wrong.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

If you're gonna do that man you gotta put it into a pattern I think, %{XXX} is the syntax for variables.

Code: [ Select ]
 
#Try to strip a slash off the end of invalid directories and redirect
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)(/)$ $1 [R,L]
 
  1.  
  2. #Try to strip a slash off the end of invalid directories and redirect
  3. RewriteCond %{SCRIPT_FILENAME} !-d
  4. RewriteRule ^([a-zA-Z0-9]+)(/)$ $1 [R,L]
  5.  


Basically I'm still trying to figure out how to strip the "/" off of a directory. I did this, and it seems to get rid of the slash, the only problem is I'm testing it on windows localhost and it attempts to redirect to this...

http://localhost/website/path/C:/Path/T ... dows/forum

When I type this: http://localhost/website/path/forum/

So I gotta figure out how to grab just the invalid directory using the hostname.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

I figured out more earlier.

Code: [ Select ]
 
#Try to strip a slash off the end of invalid directories and redirect
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z0-9-_]+)/$ http://%{HTTP_HOST}%{SCRIPT_URL}/$1 [R=301,NC,L]
 
  1.  
  2. #Try to strip a slash off the end of invalid directories and redirect
  3. RewriteCond %{SCRIPT_FILENAME} !-d
  4. RewriteRule ^([a-z0-9-_]+)/$ http://%{HTTP_HOST}%{SCRIPT_URL}/$1 [R=301,NC,L]
  5.  


Seems SCRIPT_URL works like crap on localhost if your web path isn't the same as your localhost path with a virtual host.
  • nalorin
  • Born
  • Born
  • nalorin
  • Posts: 1

Post 3+ Months Ago

You should be able to strip the trailing slash off with:
Code: [ Select ]
RewriteRule ([a-z0-9-_]+)/$ $1 [R,NC,L]

I know it's been a long time since this thread has been active... but ya never know if the problem's been solved unless it says it's been solved ;)

edit:
Note that the RewriteRule does not have a ^ at the beginning. This should work to match any directory (like localhost/dirname/blah1/blah2/) and not just localhost/dirname/
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

nalorin wrote:
but ya never know if the problem's been solved unless it says it's been solved ;)


I'm confused, didn't it say SOLVED?

What I have currently works. Thanks anyway though at the attempt. I actually ended removing the R=301.

Even so your method doesn't work for what I need, it will try to redirect to a crazy path if you put a slash on the end, just so you know. So if you go to http://sdflkjsdfdslf.com/hello/ and your web server is a windows machine, it will redirect you to something like http://sdflkjsdfdslf.com/C:/path/to/rea ... tory/hello, which is bad. :(

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 85 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.