Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
mod_rewrite newb question...[solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Mon Jul 18, 2005 7:19 pm    Post subject: mod_rewrite newb question...[solved] Reply with quote

Hi there
I want the following to take place:
Translate the url:
http://www.mypage.com/index.php?page=news
to something like:
http://www.mypage.com/index/news/

I tried to achieve this with the following entry in my <Directory> part of my /etc/apache2/vhosts.d/mypage.conf:
Code:

RewriteEngine On
Rewrite Rule ^/(.+)/$ index.php?page=$1 [L]
# Protect the .htaccess-File
RewriteRule  ^\.htaccess$ - [F]


How do I have to understand the "Rewriting" of the url?... should it not be that when I access a page through my index.php like
Code:
<a href="index.php?page=news"></a>
<?php
$page = $_GET["page"];
if (!isset($page)) { $page = "main"; }
$page = "./include/".$page.".php";
include ($page);
?>


I should see instead of
http://www.mypage.com/index.php?page=news
an url like this one
http://www.mypage.com/index/news/
in my browser?...
How could I achieve such an url translation, whereas the server internally parses the php code and shows an url like the above?...
The Problem is, nothing is happening at all, even though mod_rewrite is loaded (phpinfo shows!...). I still see exactly the same address in my client browser...


Last edited by fidel on Wed Jul 20, 2005 9:05 am; edited 1 time in total
Back to top
View user's profile Send private message
Camp
Tux's lil' helper
Tux's lil' helper


Joined: 31 May 2004
Posts: 82
Location: Moscow

PostPosted: Tue Jul 19, 2005 3:11 am    Post subject: Reply with quote

try
Code:
Rewrite Rule ^index/(.+)$ index.php?page=$1 [L,QSA]

_________________
registered Linux user #357267
GENTOO
Back to top
View user's profile Send private message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Tue Jul 19, 2005 5:44 am    Post subject: Reply with quote

Thanks for the tipp! Unfortunately though still the same, :? nothing changes at all....
any ideas?...
Back to top
View user's profile Send private message
Camp
Tux's lil' helper
Tux's lil' helper


Joined: 31 May 2004
Posts: 82
Location: Moscow

PostPosted: Tue Jul 19, 2005 11:35 am    Post subject: Reply with quote

Oh, fidel, BIG sorry for my ( or your :lol: ) error
RewriteRule in one word !
Code:
RewriteEngine On
RewriteRule ^index/(.+)$ index.php?page=$1 [L,QSA]

_________________
registered Linux user #357267
GENTOO
Back to top
View user's profile Send private message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Tue Jul 19, 2005 12:47 pm    Post subject: Reply with quote

No need for sorries!...
hmm, this actually seems quite similar to me!?!....
RewriteEngine On
has been set before!...
I tried things like

Code:
RewriteEngine On
RewriteRule ^index/(.+)$ index.php?page=$1 [L]


and...
Code:
RewriteEngine On
RewriteRule ^index/(.+)$ index.php?page=$1 [QSA,L]


and...
Code:
RewriteEngine On
RewriteRule ^/index/(.+)$ index.php?page=$1 [L]


and so on... but with the url nothing happens at all....->?... do I need to include the pages in another way than in my first posting to have the rule take affect?...
Thanks for any help!
Back to top
View user's profile Send private message
ronnie
n00b
n00b


Joined: 21 Jul 2004
Posts: 66

PostPosted: Tue Jul 19, 2005 2:44 pm    Post subject: Reply with quote

How about:
Code:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^page=(.*)$
    RewriteRule ^/index.php$ index/%1/ [R]

Also, are you sure that url rewrites works at all from your config? Start with a simple one like
Code:
  RewriteEngine On
  Rewrite Rule ^/foobar.php index.php [R]
Back to top
View user's profile Send private message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Tue Jul 19, 2005 3:03 pm    Post subject: Reply with quote

Thanks a lot! I will try that this evening!.. and post the effects of course! ;-)
Back to top
View user's profile Send private message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Tue Jul 19, 2005 4:16 pm    Post subject: Reply with quote

Really bizarre!..
Trying
Code:
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^index.php$ index/%1/ [R]


delivers an url like
....index/news/?page=news
and leads to a 404 Error...

Do I completely misunderstand the functionality of mod_rewrite? Can I achieve my goal at all with this module?... I begin to hesitate!.. :?

Ronnie: Thanks for the tip! This way finally somethings happens! ;-) What I don't get though, after the RewriteRule, the first argument beginning with the ^, is that not the "new" url and the second argument (here: index/%1/) the "old" url that should get replaced? What now happens is the opposite around I guess... And, what does the %1 stand for?.. is this the {QUERY_STRING}?...
Thanks again for any help!!!!
Back to top
View user's profile Send private message
ronnie
n00b
n00b


Joined: 21 Jul 2004
Posts: 66

PostPosted: Tue Jul 19, 2005 5:04 pm    Post subject: Reply with quote

fidel wrote:
Really bizarre!..
Trying
Code:
RewriteCond %{QUERY_STRING} ^page=(.*)$
RewriteRule ^index.php$ index/%1/ [R]


delivers an url like
....index/news/?page=news
and leads to a 404 Error...

...which is exactly what you where after, except for the query part. At least that was how I understood the original question. I'm not sure why the query string is appended to the rewritten path. Do some more reading, my example was more or less from my head.
The 404 is probably because you don't have the folder /index/news/

fidel wrote:
Do I completely misunderstand the functionality of mod_rewrite? Can I achieve my goal at all with this module?... I begin to hesitate!.. :?

Remember that mod_rewrite sends a "HTTP Location" header response telling the client to make a new request to the URL given in the response.
fidel wrote:
Ronnie: Thanks for the tip! This way finally somethings happens! ;-) What I don't get though, after the RewriteRule, the first argument beginning with the ^, is that not the "new" url and the second argument (here: index/%1/) the "old" url that should get replaced? What now happens is the opposite around I guess... And, what does the %1 stand for?.. is this the {QUERY_STRING}?...
Thanks again for any help!!!!

%1 is a back reference to the regular expression match (everything matching inside parantheses) in RewriteCond.

Good luck!
Back to top
View user's profile Send private message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Tue Jul 19, 2005 6:46 pm    Post subject: Reply with quote

Ah!... so %1 is a backreference for the regular expression in RewriteCond and $1 is a backreference for the regular expression in a RewriteRule!?..
So far so good!...
With the [R] at the end, mod_rewrite really seems to redirect the url, so far so good. The reason for my wish to use mod_rewrite is another though...
I want the following to take place:

I've got a site written in php, not a big one, I just want to keep it easily to maintain. Therefore I want to include some stuff into my index.php (navigation etc.). The page appears as a dynamic one though (obviously: index.php?page=news etc.). I would like to have my server to parse those php-includes BUT! to show a different url than it originally would. So instead of index.php?page=news something like index/news or index/news/ or index/news.html without having a real html-page in a folder index.
Here my question comes:
Is that possible at all?.. I thought, the mod_rewrite is here for!? "Rewriting" urls.... Or is it just that I can redirect specific urls to another (but existing) place?

.. and again: Thanks a lot for any help!!!
Greets
fidel
Back to top
View user's profile Send private message
fidel
Guru
Guru


Joined: 16 Jul 2004
Posts: 407
Location: CH

PostPosted: Wed Jul 20, 2005 8:55 am    Post subject: Reply with quote

:oops: :oops: :oops: Stupidstupistupidme!!!! aarghh, Sorrysorrysorry for all that stupid me!!!
Of course I misunderstood the how it works!!... It does work!!! The url index.php?page=news works AS WELL as the url /news/... that's the whole clue! So I don't need to link a site like
Code:
<a href="index.php?page=news"></a>


But like
Code:
<a href="news/"></a>


The mod_rewrite really "rewrites" the url, IF an url gets requested that matches a rule, according to the specific rule.... so if index.php?page=news does not match the rule, it stays the same. If /news/ matches the rule, it gets translated to index.php?page=news and therefore treated and parsed as wanted.
My fully working and making me happy mod_rewrite section in my /etc/apache2/vhosts.d/mypage.conf file:

Code:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)/$ index.php?page=$1 [L]

# protect the htaccess-file
RewriteRule  ^\.htaccess$ - [F]


It's probably not a good idea to set the end of an url as a /, but since I only got a small page that doesn't need to have any deeper directories I like to have it that way.

Thanks for your help and please sorry for that stupid me!!!!! :oops: :oops: :oops:
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum