School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,215 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,574 people online right now. Registration is fast and FREE... Join Now!




.htaccess files and changing what the URL looks like

 

.htaccess files and changing what the URL looks like, (hope this is in the best sub forum!

eddieboy665

7 Oct, 2007 - 08:24 AM
Post #1

D.I.C Head
**

Joined: 9 Apr, 2007
Posts: 113



Thanked: 8 times
My Contributions
Hi,

I have just finished laying out a university society website:
www.exeterflying.co.uk

I am trying to learn how to use .htaccess files to change the appearence of the URL.

My aim is to change the pages URLs which currently work like so:
Home page (also works as above): www.exeterflying.co.uk/index.php?p=home
And another example - Contact page: www.exeterflying.co.uk/index.php?p=contact

And to change them so that is the real URL but in the address bar in the browser they look somehting like this:
www.exeterflying.co.uk/home
and: www.exeterflying.co.uk/contact

I pressume I am right in thinking I should be using a .htaccess file and have got close to the result but it has always ended up redirecting the URL, ie changing it in the address bar when i get to the page!

Some of the combinations I have found and tried from google:
CODE

RewriteEngine on
rewriterule ^[^/]+/(.*) /index.php?p=$1 [R,L]

RewriteRule ^[^/)?$ index.php?p=$1[R,L]

RewriteEngine on
rewriterule $(/)[a-z]+ /index.php?p=$1 [R,L]

RewriteEngine on
RewriteRule ^/contact$ /index.php?p=contact[nc]

RewriteEngine On
RewriteRule ^(http://www.exeterflying.co.uk/[A-Za-z0-9-]+)/?$ index.php?page=$1


Am I on the right lines? Can anyone give me any pointers? I have already read most of the Apache page on this and either didn't fully understand it or just didn't apply to this.

Cheers,
Ed

User is offlineProfile CardPM
+Quote Post


girasquid

RE: .htaccess Files And Changing What The URL Looks Like

7 Oct, 2007 - 10:17 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 59 times
Dream Kudos: 825
My Contributions
Try something like this:
CODE

RewriteEngine On
RewriteRule ^(.*)$ /index.php?p=$1 [NC,L]

That will redirect anything after the slash to index.php?p=<text>. If you'd like to set up keyword-specific adjustments, do something like this:
CODE

RewriteEngine On
RewriteRule ^contact(/)?$ /index.php?p=contact
RewriteRule ^home(/)?$ /index.php?p=home

I don't actually know for certain if you need the (/)? portion of the regular expression, but I figure it doesn't hurt.
User is offlineProfile CardPM
+Quote Post

eddieboy665

RE: .htaccess Files And Changing What The URL Looks Like

7 Oct, 2007 - 11:20 AM
Post #3

D.I.C Head
**

Joined: 9 Apr, 2007
Posts: 113



Thanked: 8 times
My Contributions
Thanks a lot, thats excellent!

The first one keeps causing an internal server error but possibly thats something to do with the upload?! But the other one works and that is great. Got any links that I could read to understand it fully?

Thanks again,
Ed
User is offlineProfile CardPM
+Quote Post

girasquid

RE: .htaccess Files And Changing What The URL Looks Like

7 Oct, 2007 - 11:56 AM
Post #4

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 59 times
Dream Kudos: 825
My Contributions
Do you understand regular expressions(regex)?

If you don't, check out this website for some information about them.

Basically the RewriteRule's are regular expressions, and what I'm doing is capturing case where the word 'contact' or 'home' is the only extra on the server path - if you try connecting to the site using something like yoursite.com/home/test, it will throw an error(or perform strangely).
User is offlineProfile CardPM
+Quote Post

snoj

RE: .htaccess Files And Changing What The URL Looks Like

7 Oct, 2007 - 01:47 PM
Post #5

Now with 10% more nom!
Group Icon

Joined: 31 Mar, 2003
Posts: 3,388



Thanked: 31 times
Dream Kudos: 775
My Contributions
Moving to Building your Site.
User is offlineProfile CardPM
+Quote Post

_net

RE: .htaccess Files And Changing What The URL Looks Like

18 Oct, 2007 - 10:04 AM
Post #6

D.I.C Head
**

Joined: 22 Sep, 2007
Posts: 146



Thanked: 1 times
My Contributions
Darn thank you man for creating this thread. This has bugged my head too much. biggrin.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:02PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month