Jump to content

[SOLVED] Nicer looking URLs


phil88

Recommended Posts

First time I've ever used mod_rewrite, but I would quite like to use it for this website I'm making.

 

I have 1 file that controls the entire website, index.php, it calls on numerous other files to create pages.

 

index.php can be set to different pages like so;

index.php?page=home

index.php?page=about

index.php?page=image

etc.

If $page=image, then there is another $_GET variable that is used, $id. So this means, the 'image' page is dependant on an extra number like so;

index.php?page=image&id=38

 

What I would like to do is, have

 

www.myurl.com/index/home go to www.myurl.com/index.php?page=home

www.myurl.com/index/about go to www.myurl.com/index.php?page=about

etc, AND

www.myurl.com/index/image/23 go to www.myurl.com/index.php?page=image&id=23

 

 

How on earth do you do that with mod_rewrite? I'm assuming that is what you need to do for it to work? It would be even better if you could miss out the /index/ part of the nice URLs altogether if that is possible, so;

myurl.com/home would actually be myurl.com/index.php?page=home

and

myurl.com/image/23 would be myurl.com/index.php?page=image&id=23

 

If that's possible. Thanks :)

 

I had a go and came up with;

RewriteEngine on

RewriteRule ^index/([A-Za-z0-9-]+)/([0-9]+)/?$ index.php?page=$1&id=$2 [L]

 

But that just gave me a "Server error!" error...like I said, this is the first time I've tried mod_rewriting :P

Link to comment
Share on other sites

At this early stage it might be worth considering what the other options are -

 

There's no mandatory requirement for the WWW prefix which leaves you room to play with virtual hosts UNLESS your domain name does not support wildcards. Most dynamic DNS sites do - I use no-ip.com and such vhosts on my own site.

 

A more elegant solution would be to use Virtual hosts in the following fashion as a primary access

 

home.mysite.com

image.mysite.com

about.mysite.com

 

You can then ALSO have www.mysite.com/about (etc.) by using a simple index.php redirect from stub folders (as you requested). Create root folders home, about and image in your doc root. In each of them place an index.php file (enable this as an "index file" using the DirectoryIndex" directive)

 

e.g. index.php file in .../document root/home

 

<?
header("Location: http://home.mysite.com");
exit;
?>

 

This is also a handy method of protecting CSS etc. folders from casual snoopers and browsers

 

The remaining part would require creation of vhost config sections in the main server config. Quite easy to do and adds trivial server overhead.

 

Think it over. I always like to have a thunk and go for a nice, easy, extendible solution which involves minimal maintenance. Mod_rewrite is great but should not always be the first choice.

Link to comment
Share on other sites

RewriteEngine on

RewriteRule ^([A-Za-z0-9-]+)/$ index.php?page=$1 [L]

RewriteRule ^images/([0-9]+)/$ index.php?page=images&id=$1 [L]

 

 

If i understand you correctly, that is what you wanted ?

 

the first rewrite rule will match www.myurl.com/aboutus/ and call index.php?page=aboutus

this way you can keep adding in more pages as you need.

 

the second rule is for the images directly, and it will only match if the url is in the format www.myurl.com/images/23/ (with the backslash)

 

 

Link to comment
Share on other sites

RewriteEngine on

RewriteRule ^([A-Za-z0-9-]+)/$ index.php?page=$1 [L]

RewriteRule ^images/([0-9]+)/$ index.php?page=images&id=$1 [L]

 

 

If i understand you correctly, that is what you wanted ?

 

the first rewrite rule will match www.myurl.com/aboutus/ and call index.php?page=aboutus

this way you can keep adding in more pages as you need.

 

the second rule is for the images directly, and it will only match if the url is in the format www.myurl.com/images/23/ (with the backslash)

 

 

That's exactly what I needed. Thanks a lot.

Link to comment
Share on other sites

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.