I have a URL that sometimes has one or more parameters and I want to be able to rewrite accordingly.
The URLs are as follows:
/index.php?pagetitle=Home
I have managed to get this URL to display how I want using the following:
/Home
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([0-9a-zA-Z]+) index.php?pagetitle=$1 [NC,L]
I would also like to be able to have the same URL, but with article ID
.com/index.php?pagetitle=Home&articleid=20
which i would like to display like this:
.com/Home/20
But I can't seem to figure out how to get it to work on more than one parameter.
UPDATE: Ok so i managed to get the below to work using : index.php?pagetitle=$1&articleid=$2
Which displays as: .com/Home/20
I have tried to take it a step further and be able to use the article title as well the rule i tried is as follows.
RewriteRule ^([0-9a-zA-Z]+)(?:/([0-9a-zA-Z]+))(?:/([0-9]*))?/?$ index.php?pagetitle=$1&articletitle=$2&articleid=$3 [NC,L,QSA]
So would display as : .com/Home/ArticleTitle/20
But it doesnt want to work for me does any one know what i am doing wrong and thanks again in advance.
http://karpium.co.uk/Home/Baits/20 - This Url Works
http://karpium.co.uk/Home - But this url doesnt work where as it does when just using the pagetitle= and articleid=
You can use the following Rule :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([0-9a-zA-Z]+)(?:/([0-9]*))?/?$ index.php?pagetitle=$1&articleid=$2 [NC,L,QSA]
Ok so seems like i have fixed it using 3 different rewrite rules which are as follows:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([0-9a-zA-Z]+)$ index.php?pagetitle=$1 [NC,L,QSA]
RewriteRule ^([0-9a-zA-Z]+)(?:/([0-9a-zA-Z]+))?/?$ index.php?pagetitle=$1&articletitle=$2 [NC,L,QSA]
RewriteRule ^([0-9a-zA-Z]+)(?:/([0-9a-zA-Z]+))(?:/([0-9]*))?/?$ index.php?pagetitle=$1&articletitle=$2&articleid=$3 [NC,L,QSA]
If any one can shed some light onto a few parts of the regex for me it would be very much appreciated:
What do the following mean?
?:/
?/?
Thanks again for all the help :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.