Jump to content

Not understanding mod_rewrite


JesseToxik

Recommended Posts

hey all,

 

In honesty I have looked at tutorials for creating "pretty links" and I can honestly say I do not understand how to use it.

I am using a navigation system that includes requested files using a URL like http://mysite.com?page=home. What I would like to do is make the links appear as http://mysite.com/page/home. How can I achieve this? I am not asking for the code to be written for me, just help in the right direction. Thanks

Link to comment
https://forums.phpfreaks.com/topic/278965-not-understanding-mod_rewrite/
Share on other sites

This is my .htaccess file.

 

ErrorDocument 404 http://google.com // Google just to test it
RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
#LoadModule rewrite_module modules/mod_rewrite.so #AddModule mod_rewrite.c
RewriteRule ^page/([0-9]{4})$ /index.php?page=$1 [L]

RewriteRule ^page/([a-z0-9]+)$ /index.php?page=$1 [L]
That would match your /page/home example and translate it into /index.php?page=home. As far has making it look like /page/home in the browser, you just need to type in the URL that way. When you create your links you need to create them using the new friendly format, not the older query-string format.

 

<a href="/page/home">Go Home</a>
not

<a href="/index.php?page=home">Go Home</a>

I tried adding what you suggested to my .htaccess  and it didnt work.

 

This is what I have.

 

ErrorDocument 404 http://mysite.com/404.php
RewriteEngine On
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
#LoadModule rewrite_module modules/mod_rewrite.so #AddModule mod_rewrite.c
RewriteRule ^page/([a-z0-9]+)$ /index.php?page=$1 [L]

Your first RewriteRule

RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
is going to match the /pages/home url first and send the request over to /pages/home.php. You need to either disable that rule or change their orders so the other one matches first.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.