Pierce McGeough
Pierce McGeough

Reputation: 3086

Use htaccess for a redirect

I am using the following code to change all my pages from .php to a simple /

RewriteRule ^(.*)/$ $1.php [L]

Now come up as www.site.com/clubs/ or www.site.com/training/ instead of www.site.com/clubs.php and www.site.com/training.php

Now within clubs i want to show the clubs (using data from a database) and the page will show as www.site.com/clubs/my-club/
All the my-club data will be in the database including the link in a url field.

I can get this with the following code. However when i echo using the $_GET['club'] method it is showing up my-club.php instead of just my-club.

RewriteRule ^clubs/([^/]*)$ clubs.php?url=$1 [L]

The ending / in the url seems to get changed into .php

Upvotes: 1

Views: 49

Answers (2)

mkjasinski
mkjasinski

Reputation: 3098

Try this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z\-]+?)/([0-9a-zA-Z\-]+?)?$ $1.php?url=$2

Whan are you run:

http://localhost/club/

result will be

http://localhost/club.php

Whan are you run:

http://localhost/club/my-club

result will be

http://localhost/club.php?url=my-club

Upvotes: 0

ulentini
ulentini

Reputation: 2412

You can achieve what you need in different ways, however that's how I would do it. Change rules order, like this:

RewriteRule ^clubs/([^/]*)/$ clubs.php?url=$1 [L]
RewriteRule ^(.*)/$ $1.php [L]

I added the trailing slash to the first rule, since I guess is part of the pattern. If I'm wrong about it you can just delete it.

If you want to append any query string from the original request URL you should use QSA together with L, so [L,QSA]

Upvotes: 2

Related Questions