Forum Moderators: phranque

Message Too Old, No Replies

.htaccess problem

friendly urls

         

Malmo

3:08 pm on Sep 6, 2010 (gmt 0)

10+ Year Member



Hi guys!

I am trying to create friendly url with a .htaccess file, but I canīt get it to work.
Here are some examples of my links that I am trying to rewrite:

www.mydomain.com/index.php?page=soccer&id=3
www.mydomain.com/index.php?page=tennis&id=4
www.mydomain.com/index.php?page=basketball&id=1
www.mydomain.com/index.php?page=icehockey&id=2

and this is want I want to achieve:

www.mydomain.com/soccer/3
www.mydomain.com/tennis/4
www.mydomain.com/basketball/1
www.mydomain.com/icehockey/2

I have tried with something like this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/([0-9]+)\.html$ index.php?page=$1-$2

I thank everyone in advance for any help I can get.
Best regards.

Brett_Tabke

5:13 pm on Sep 6, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You are on the right track. Try something like this for the effective rule line:

RewriteRule ^soccer/([0-9\-]+) index.php?page=$1&id=$2 [T=application/x-httpd-cgi,L]

or

RewriteRule ^(.*)/([0-9\-]+) index.php?page=$1&id=$2 [T=application/x-httpd-cgi,L]

Malmo

6:53 am on Sep 10, 2010 (gmt 0)

10+ Year Member



Thank you Brett_Tabke for the help. I have another question and that how can I change this link www.mydomain.com/index.php?page=soccer&id=3 into www.mydomain.com/soccer/england/3.

jdMorgan

11:04 am on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must edit your HTML pages -- or in this case, you must edit the script that generates your HTML pages, so that the links on your pages refer to the friendly URLs. URLs are created and defined by the links on Web pages, and the server cannot "change" them.

Jim

Malmo

6:59 am on Sep 13, 2010 (gmt 0)

10+ Year Member



Thanks Jim for the solution it works like I want it now, but I have still one last question that I can―t figure out and that is how to setup it from a form with hidden fields. For example I have this form:
The RewriteRule would probably be like this:
RewriteRule ^password/([^/]+)/([^/]+) index.php?page=password&mail=$2 , but with the link I have no clue

<form method="get" action="index.php">
<input type="hidden" name="page" value="password" />
<input type="text" name="mail">

<input type="submit" name="submit" value="Send e-mail">
</form>
When the user clicks on send the link is then index.php?page=password&mail=(the e-mail address that was entered)&submit=send+e-mail.

Best regards.
Malmo

jdMorgan

4:25 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot use the GET method from your form, because by definition, it relies on query strings.

Look into using a POST method instead if this is very important to you.

Jim