Jump to content

[SOLVED] Clean URLs


canadabeeau

Recommended Posts

Hi rhody_korb,

 

I assume Joomla already makes use of mod_rewrite for the rewriting of all it's URL's, this is why you don't use it when using Joomla.

 

If you want to write your own PHP website, and use the URL format you require, you will need to use mod_rewrite.

 

The tutorial I linked to above includes code snippets.

Link to comment
Share on other sites

Link to comment
Share on other sites

Hi rhodry_korb,

 

The tutorial I posted has all the information you need to start off with mod_rewrite, but I'll post some code for you.

 

Firstly, you need to create an .htaccess file, and paste the following code into it:

 

###################################################
# Turn the RewriteEngine on.                      #
###################################################

RewriteEngine on

###################################################
# Add a leading www to domain if one is missing.  #
###################################################
# If this rule is used, the rewriting stops here  #
# and then restarts from the beginning with the   #
# new URL                                         #
###################################################

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

###################################################
# Do not process images or CSS files further      #
###################################################
# No more processing occurs if this rule is       #
# successful                                      #
###################################################

RewriteRule \.(css|jpe?g|gif|png)$ - [L]

###################################################
# Add a trailing slash if needed                  #
###################################################
# If this rule is used, the rewriting stops here  #
# and then restarts from the beginning with the   #
# new URL                                         #
###################################################

RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]

###################################################
# Rewrite web pages to one master page            #
###################################################
# /somepage/            => master.php             #
#                            ?page=somepage       #
# /somesection/somepage => master.php             #
#                            ?section=somesection #
#                            &page=somepage       #
# /somesection/somesub/somepage/                  #
#                       => master.php             #
#                            ?section=somesection #
#                            &subsection=somesub  #
#                            &page=somepage       #
###################################################
# Variables are accessed in PHP using             #
# $_GET['section'], $_GET['subsection'] and       #
# $_GET['page']                                   #
###################################################
# No more processing occurs if any of these rules #
# are successful                                  #
###################################################

RewriteRule ^aboutus/ /index.php?uri=aboutus [L]
RewriteRule ^contactus/ /index.php?uri=contactus [L]
RewriteRule ^anotherpage1/ /index.php?uri=anotherpage1 [L]
RewriteRule ^you-can-use-hyphens/ /index.php?uri=toseparatewords [L]

 

Now save the .htaccess file and upload it to the directory your index.php file is in.  You can now navigate to http://www.domainofmine.com/aboutus and http://www.domainofmine.com/index.php?uri=aboutus will load.

 

The above code is fully commented so should be easy to follow.

Link to comment
Share on other sites

I am sure wikipedia would not write out

RewriteRule ^aboutus/ /index.php?uri=aboutus [L]
RewriteRule ^contactus/ /index.php?uri=contactus [L]
RewriteRule ^anotherpage1/ /index.php?uri=anotherpage1 [L]
RewriteRule ^you-can-use-hyphens/ /index.php?uri=toseparatewords [L]

into their .htaccess, thankyou bricktop but does anyone know another way that I don't have to do the above to acieve the wikipedia style URL

Link to comment
Share on other sites

Hi rhodry_korb,

 

You're right in that they wouldn't write everthing out like that, this is why you need to read the tutorials.  Dynamic URL rewriting happens because the PHP application is written in that way.

 

For example, the following Rewrite rule:

 

RewriteRule ^news/category/([^/\.]+)/?$ /news.php?f=shownews&category=$1 [L]

 

Will dynamically rewrite any news URL based on its category.  So the one rule will allow for any number of news categories such as:

 

http://www.domain.com/news/category/headlines

http://www.domain.com/news/category/world

http://www.domain.com/news/category/weather

http://www.domain.com/news/category/sport

 

You need to read up on mod_rewrite and look at how it functions, it's an extremely powerful and complex tool, the code I supplied you is merely a very basic mod_rewrite usage.

Link to comment
Share on other sites

so how would i set $1

RewriteRule ^wiki/([a-zA-Z]+)$ /wiki.php?id=$1

for my situation

where

http://en.wikipedia.org/wiki/Moodle or in my case

http://www.domainofmine.com/aboutus which is really like

http://www.domainofmine.com/index.php?uri=aboutus (my current way, but looks messy) or

http://www.domainofmine.com/index.php/aboutus.html (http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/)

 

could you please help once more keldorn or someone else

Link to comment
Share on other sites

You probably will want to create an imaginary folder that doesn't exist, so decide on how you want your urls.

 

For example you could haave

 

example.com/company/about

 

 

The reason why you will want it like that is so that your mod_rewrite doesn't interfere with your real folders.

So say you have an index.php file that take 3 pages,  ?page=about  ?page=contact  page?privacy

 

You would use this regex in your mod rewrite.

 

RewriteRule ^company/([a-zA-Z]+)$ /index.php?page=$1

 

These urls now would rewrite to your index..or whatever file.

 

example.com/company/contact

example.com/company/about

example.com/company/privacy

 

There are more things you can do with different regurarl expressions, that just matches letters. You could match numbers also.  Some people prefer to match the numbers instead, where that number correlate to a auto-incrmenting id in their mysql table, and the words in the url are just for show and don't anything.

An example of that would something like

example.com/68/hello-world.html . The mod_rewrite would ignore the everything except the number and transfer that to the script like  article.php?id=68

 

I hope this all makes sense becuase I cannot explain any more than that.

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.