Simple mod_rewrite question

This is a discussion on Simple mod_rewrite question within the Apache Web Server forums, part of the Web Server and Related Forums category; Hello. I'm trying to make use of "pretty URLs" with Apache's rewrite module. Here's what ...


Go Back   Usenet Forums > Web Server and Related Forums > Apache Web Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-13-2008
k3pp0
 
Posts: n/a
Default Simple mod_rewrite question

Hello.


I'm trying to make use of "pretty URLs" with Apache's rewrite module.

Here's what I want:

http://foo.bar/index.php?download

should be replaced with

http://foo.bar/download


My .htaccess looks like that:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php


In the index.php I look at $_SERVER['REQUEST_URI'] and act
accordingly:

$requestedURI = empty($_SERVER['REQUEST_URI']) ? false :
$_SERVER['REQUEST_URI'];

switch($requestedURI) {
case "/download":
include "download.php";
break;
default:
// do something
break;
}

This works if I go to http://foo.bar/download - it shows index.php
with the included download.php file. But if I go to http://foo.bar/download/
(with a slash at the end of the URL) it doesn't do anything (default
case of switch($requestedURI)), because "/download" != "/download/".

To solve this I could just make two cases: case "/download" and case "/
download/", but I don't really like this solution, if there's a better
one, please tell me.

Let's take it I use the method with the two cases ("/download" and "/
download/"), there's another problem:
In the index.php I've included a stylesheet: "<link rel="stylesheet"
type="text/css" href="main.css" />". As you can see, I use a relative
link to the css file. So if I call http://foo.bar/download, it loads
the css file from http://foo.bar/main.css, but if I enter http://foo.bar/download/
(with the slash) it can't find the css file, because it tries to
locate it at http://foo.bar/download/main.css, but of course, there is
no main.css, /download/ is not even a directory... How do I fix this?

Thanks in advance for any help!
  #2 (permalink)  
Old 04-16-2008
k3pp0
 
Posts: n/a
Default Re: Simple mod_rewrite question

Anyone?
  #3 (permalink)  
Old 04-20-2008
k3pp0
 
Posts: n/a
Default Re: Simple mod_rewrite question

So I'm trying to only use the RewriteEngine to handle my url.

I came across this problem:

Let's assume I have a url like that:
http://foo.bar/index.php?site=users&...&action=addnew

For example, this url would open a site that let's you add a new
user.
The "pretty" url should look like http://foo.bar/users/manage/addnew
This works fine:
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?site=$1&mode=
$2&action=$3 [L]

But http://foo.bar/index.php?site=users&mode=manage (note that
there's
no action parameter) should also be accessible through the pretty url
(/users/manage) and so should http://foo.bar/index.php?site=users
(through /users). The RewriteRule I mentioned above does only work if
you have a site, mode and action parameter, but not with just a site
or just a site + mode parameter.

Is there a way to solve this without making a RewriteRule for each
case (or probably a better solution than mine)?

Thanks in advance!
  #4 (permalink)  
Old 04-20-2008
HansH
 
Posts: n/a
Default Re: Simple mod_rewrite question

"k3pp0" <upthekhyber@gmail.com> schreef in bericht
news:6839a859-e6ad-43e7-b590-7cd2f5d88c05@a22g2000hsc.googlegroups.com...

> This works fine:
> RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$
> index.php?site=$1&mode$2&action=$3 [L]
>
> But http://foo.bar/index.php?site=users&mode=manage
> (note that there's no action parameter) should also be
> accessible through the pretty url (/users/manage) and
> so should http://foo.bar/index.php?site=users (through
> /users).
> The RewriteRule I mentioned above does only work if you
> have a site, mode and action parameter, but not with just
> a site or just a site + mode parameter.
>
> Is there a way to solve this without making a RewriteRule
> for each case (or probably a better solution than mine)?
>

Try on a single line the untested
RewriteRule ^([^/\.]+)(/([^/\.]+))?(/([^/\.]+))?/?$
index.php?site=$1&mode=$3&action=$5 [L]

HansH


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0