mod_rewrite: redirect all requests to index.php
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Web Servers reviews > Apache Server configuration support > mod_rewrite: redirect all requests to index.php




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    mod_rewrite: redirect all requests to index.php  
listbot@gmx.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:18 PM

Hi,

I want to add some php scripting features to an existing site, which
was build with pure html. My idea is to redirect all requests to the
site to an index.php, that acts as a filter. That index.php will do
something based on the filename.

So all existing links like

some_name.html
/somedir/some_name.html

some_name.htm
/somedir/some_name.htm

should become:

/index.php?page=some_name.html
/index.php?page=/somedir/some_name.html


how can this be done with mod_rewrite?



Thanks you very much for your attention!














[ Post a follow-up to this message ]



    Re: mod_rewrite: redirect all requests to index.php  
Davide Bianchi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:18 PM

listbot@gmx.de wrote:
quote:
> how can this be done with mod_rewrite?
Read the documentation of mod_rewrite, there are a couple of examples that does exactly this. Davide




[ Post a follow-up to this message ]



    Re: mod_rewrite: redirect all requests to index.php  
listbot@gmx.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:19 PM

On 28 Nov 2003 10:57:47 GMT, Davide Bianchi
<davideyeahsure@onlyforfun.net> wrote:
quote:
>Read the documentation of mod_rewrite, there are a couple of examples >that does exactly this.
I found this, posted some days ago here: RewriteEngine on RewriteRule ([^/]+).html index.php?url=$1 [L] but I get a "bad request" error when trying this... how to debug? What´s wrong with that?




[ Post a follow-up to this message ]



    Re: mod_rewrite: redirect all requests to index.php  
listbot@gmx.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:19 PM

On Fri, 28 Nov 2003 12:18:14 +0100, listbot@gmx.de wrote:
quote:
>RewriteEngine on >RewriteRule ([^/]+).html index.php?url=$1 [L]
When i write RewriteRule ([^/]+).html index.php?url=$1 it seems to work, but variables are not sent, so on a request like some_page.html?session_id=1213 the session_id variable is lost... any solutions to this? Yes i read the documentation, but I need this feature now and I think after reading the docs and understanding them I will be retired...




[ Post a follow-up to this message ]



    Re: Re: mod_rewrite: redirect all requests to index.php  
listbot@gmx.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:19 PM

On Fri, 28 Nov 2003 12:45:49 +0100, listbot@gmx.de wrote:
quote:
>some_page.html?session_id=1213 > >the session_id variable is lost...
ok, i just found [QSA] the only problem now O have is, that directories in that request should be removed, so relative links still work. Example: with RewriteRule ([^/]+).html /index.php?url=$1 [QSA] some_page.html calls index.php?url=page but /somedir/somepage.html tries to get relativ links from /somedir/ how can I change this? THANKS!




[ Post a follow-up to this message ]



    Re: mod_rewrite: redirect all requests to index.php  
Justin Koivisto


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:19 PM

listbot@gmx.de wrote:
quote:
> On Fri, 28 Nov 2003 12:45:49 +0100, listbot@gmx.de wrote: > > > > > > ok, i just found [QSA] > > the only problem now O have is, that directories in that request > should be removed, so relative links still work. Example: > > with > > RewriteRule ([^/]+).html /index.php?url=$1 [QSA] > > > some_page.html calls index.php?url=page > > but > > /somedir/somepage.html > > tries to get relativ links from /somedir/ > > how can I change this?
Get rid of the relative links. The quickest way (not necessarily the best) of doing it is to have your php script filter through all the href and scr attributes and correct them if they are relative. Something similar to this: so, at the top of index.php, use: ob_start(); Then at the bottom after all the output, use: <?php $content=ob_get_contents(); ob_end_clean(); $site_root='/'; preg_match_all('/(src|href)="([^"]*)"/is',$content,$matches); $search=array(); $replace=array(); foreach($matches[2] as $i=>$match){ if(substr($match,0,4)!='http' || substr($match,0,1)!='/'){ // if the link doesn't start with 'http' or '/', do this $search[$i]=$matches[0][$i]; $replace[$i]=$matches[1][$i].'="'.$site_root.$match.'"'; } } echo str_replace($search,$replace,$content); ?> HTH -- Justin Koivisto - spam@koivi.com PHP POSTERS: Please use comp.lang.php for php related questions, alt.php* groups are not recommended.




[ Post a follow-up to this message ]



    Re: Re: mod_rewrite: redirect all requests to index.php  
James


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-04 12:19 PM

I don't know for sure, nor have I tested this, but I would imagine that
something like this would work:

RewriteRule ([/]+).html /index.php?url=$1 [QSA]

Notice that I removed the carat (^).  The carat indicates 'that begins
with'.  So you were saying, take all information beginning with the very
first /.  Perhaps (but I do not know for sure) this new code will take it
from the final /.  I could very well be wrong, but at the very least I think
that this is the direction that the logic will take.


James


<listbot@gmx.de> wrote in message
 news:3gdesv4v08skpbtob0tmuqmlt7pgdomh0m@
4ax.com...
quote:
> On Fri, 28 Nov 2003 12:45:49 +0100, listbot@gmx.de wrote: > > > > ok, i just found [QSA] > > the only problem now O have is, that directories in that request > should be removed, so relative links still work. Example: > > with > > RewriteRule ([^/]+).html /index.php?url=$1 [QSA] > > > some_page.html calls index.php?url=page > > but > > /somedir/somepage.html > > tries to get relativ links from /somedir/ > > how can I change this? > > THANKS! > >




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:32 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register