Jump to content

Recommended Posts

hi

Could someone help with friendly url's pls. I already have a index.php page in the root and also a dynamic page called page.php - This page pulls database pages i.e contacts, cinema and so on.  (using php5). If it's any easier I can move pages into folders and re-name to index.php ?

 

My current url is

www.mysite.co.uk/page.php?page=cinema

and would like (prefer)

www.mysite.co.uk/cinema

or

www.mysite.co.uk/page/cinema

 

Thanks

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/
Share on other sites

Your in luck. I recently had to find a solution to this problem. :) Because I too prefer those sorts of URLs.

 

You need to make sure that your host or server has the "rewrite_module" enabled. If your running WAMP just click on the status bar icon, go to Apache, then go to Apache modules. Scroll down and look for rewrite_module, enable it if you haven't already done so...

 

Then, create a file called .htaccess

In that file put this:

 

RewriteEngine on

RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?page=$1&section=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ page.php?page=$1&section=$2&subsection=$3 [L]

 

The three different things allow for subsections etc etc... Hope that clears a few things up.

 

This creates pages like www.mysite.com/aboutus/

hi ProjectFear

 

This worked first time - spot-on.

It works with all directories.

 

I also have other pages in the whats on section current URL is:

 

http://www.mysite.co.uk/whats_on_article.php?id=22  - any suggestions on this ? maybe 'whats_on_article/22  ?

and

http://www.mysite.co.uk/press.php - how can I remove .php?

 

Thanks again

Just add another:

 

RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L]

 

But make it:

 

RewriteRule /whatson/^([^/\.]+)/?$ whats_on_article.php?id=$1 [L]

 

And for the other one:

 

RewriteRule ^/press/$ press.php  [L]

 

Hope thats all good, I'm still not brilliant with mod_rewrite yet.

 

EDIT:

Here is a link that may help you.

http://www.easymodrewrite.com/

thanks

 

the last two commands don't work.

 

my .htaccess is

 

RewriteEngine on


RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?page=$1&section=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ page.php?page=$1&section=$2&subsection=$3 [L]

RewriteRule /whatson/^([^/\.]+)/?$ whats_on_article.php?id=$1 [L]
RewriteRule ^/press/$ press.php  [L]

 

so if I go to url

www.mysite.co.uk/press or www.mysite.co.uk/whatson

I'm actually getting the wrong page (page.php)

 

I've also notices if I type in a page that does not exist i.e

www.mysite.co.uk/doesnotexist

I'm also being served up page.php rather than a page not found error..

 

Any clues pls

Try moving the bottom two rules to the top... In your page.php, have something like this:

 

if(!isset($_GET['page'])){
$page = "home";
}else{
$page = $_GET['page'];
}

if(file_exists("pages/".$page.".php")){
//the file is valid, include it
}else{
//404 error, include your 404 script.
}

hi again - sorry to keep this thread going -

 

I've tried swapping the two lines to the top but still it does't work. then I tried to alter

RewriteRule ^/press/$ press.php  [L] to RewriteRule ^press$ press.php  [L]  I've removed forward slashes and now mysite.com/press works.

 

I've also added this:

RewriteRule ^whatson$ whats_on_listing.php  [L]

 

but the whats_on_listing.php i'm passing variables to on some occations, these are :

 

whats_on_listing.php?viewstate=today

whats_on_listing.php?viewstate=week

whats_on_listing.php?viewstate=soon

 

I want to achieve

whatson/today

whatson/week

whatson/soon

 

Thanks

 

 

 

Okay... Try this for the whats on thing:

 

RewriteRule ^whatson/([^/\.]+)/?$ whats_on_listing.php?viewstate=$1&nextvariable=$2  [L]

 

To add more variables, you do this, I think: (:P)

RewriteRule ^whatson/([^/\.]+)/([^/\.]+)/?$ whats_on_listing.php?viewstate=$1  [L]

 

And so on...

Hi

 

I think it was working all along (most of it anyway)- The problem was that when I was trying it, I was being served a page without css and assumed it wasn't working at all.

 

Whats seems to be happening is that when I add

RewriteRule ^whatson/([^/\.]+)/?$ whats_on_listing.php?viewstate=$1&nextvariable=$2  [L]

 

to the htaccess it also alters the root folder path.

Rather than the root being www.mysite.com it changes it to  www.mysite.com/whats_on_listing.php

so the stylesheet and images cant be found.

I added ../ infront of the css file and it seems to now work, though a bit tedious. Is there something i could add in htaccess to solve this?

Yeah I have encountered this problem. Which is why I add full paths or links to my images/css files now. ;)

 

There may be a solution though, I think you can do some stuff with mod_rewrite but I'm happy with giving it full paths or links.

hi again I've got the urls to work - thanks so much

 

I'm now looking at sending a 404 error if someone goes to

 

www.mysite.com/nopage

 

you gave me the code:

if(file_exists("pages/".$page.".php")){

//the file is valid, include it

}else{

//404 error, include your 404 script.

}

 

The page.php is in the root, so I removed from the path 'pages/'

But I keep getting the 404 error

 

Is it possible to check the existance of a dynamic page in this way? as only page.php actually exists?

I'm echoing the $page variable and this gives me the correct page name.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.