James
James

Reputation: 23

htaccess rewrite with multiple queries?

I have SEO friendly urls working ok however i've since changed my file directory and need to adjust things however I can't seem to get this to work.

Essentially i'm after the following through htaccess

$1.php?id=$2&subid=$3

and possibly more queries afterwards, any ideas?

I don't fancy listing each page but would rather do it dynamically. htaccess works for the $1.php page but I cant figure out how to add in extra $'s

Options +FollowSymlinks
RewriteEngine On
RewriteBase /v2/
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1.php [L,NC]

https://steve-morris.co.uk/v2/services works but https://steve-morris.co.uk/v2/property/to-rent doesn't

essentially i'm after

https://steve-morris.co.uk/v2/property/to-rent

to link to

https://steve-morris.co.uk/v2/property.php?id=to-rent

and for

https://steve-morris.co.uk/v2/services/hello/this-is-a-service

to go to

https://steve-morris.co.uk/v2/services.php?id=hello&subid=this-is-a-service

but id like it dynamic so that I don't have to write a rewrite for each page

Upvotes: 0

Views: 71

Answers (2)

Panama Jack
Panama Jack

Reputation: 24448

Essentially you should be able to just add the additional rules.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /v2/
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?id=$2&subid=$3 [L,NC,QSA]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?id=$2 [L,NC,QSA]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1.php [L,NC,QSA]

Let me know how that works out.

Upvotes: 1

Roman
Roman

Reputation: 1308

Last lines should be something like this:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?id=$2&subid=$3 [L,NC]
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?id=$2 [L,NC]
RewriteRule ^([^/]+)/?$ $1.php [L,NC]

Upvotes: 0

Related Questions