Forum Moderators: phranque
www.mysite.com/page.php/var1/foo/var2/baa
Rather than
www.mysite.com/page.php?var1=foo&var2=baa
On the old server apache called page.php, but on my new server it doesn't, it gives a 404. Therefore I can use PHP to change URLs (got a nifty little script)
QUESTION ONE: Is there a way in .htaccess to say "call page.php rather than 404"?
Anyway, assuming no, I've decide to try and write a rewriterule to make things work. Please bear in mind that page.php may change to page2.php or foo.php etc and there may be 1, 2, 3 ... 20 arguements passed.
Anyway, I started with this
RewriteRule ^([^.]+)\.php/([^.]+)?/([^.]+)?$ pagename.php\?$2=$3
The first problem is that if we pass open somepage.php/id/5 (for example) it works. But if we call pagename.php/id/5 it doesn't, it gives a 404. I assume it's thinking it'll be stuck in a loop or something? What I really wanted to do was:
RewriteRule ^([^.]+)\.php/([^.]+)?/([^.]+)?$ $1\?$2=$3
QUESTION 2 - how do I stop it giving me a 404 whenever I call it using the same page name as where it redirects to (if that makes sense)
QUESTION 3 - easy one. I can make ([^.]+) optional by writing ([^.]+)? ... but how do I make the / optional as well? I've tried (/)?
Any help appreciated
Thanks
This old thread [webmasterworld.com] may be of assistance to you.
Jim
Surely my example is like this one in the given file:
RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?product=$1&color=$2&size=$3&texture=$4&maker=$5 [L]
My difference is I'm trying to do for all files generically, not just for product
What I have now looks like this:
RewriteRule ^([^.]+)\.html/([^.]+)/([^.]+)?$ $1\.php\?$2=$3
Basically, it works:
www.mysite.com/page.html/id/123 -> www.mysite.com/page.php?id=123
But if I change rule to
RewriteRule ^([^.]+)\.html/([^.]+)/([^.]+)?$ $1\.php\?$2=$3
And call
www.mysite.com/page.php/id/123 -> Error 404
Still think it's what you say?
Thanks for replt
For others, this is what I have done - bit clunky ... but don't know how to loop. Oh, plus it'll put?&=&=&=&= at the end if we have less than the maximum arguements
ANyway:
Options +FollowSymLinks
RewriteEngine on
# This rule will convert:
# pagename.php/ to pagename.php
# pagename.php/a/b/ccc/dd/ to pagename.php?a=b&ccc=dd
# Up to 2 arguement pairs (can be missing)
# RewriteRule ^([A-Za-z0-9_-]+)\.php/([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?$ /$1\.php\?$2=$3&$4=$5
# Now rewrite it for 20 arguement pairs instead
RewriteRule ^([A-Za-z0-9_-]+)\.php/([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?
['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?
['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?
['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?['/']?([^/]+)?
['/']?([^/]+)?['/']?$ /$1\.php\?$2=$3&$4=$5&6=$7&8=$9&10=$11&12=$13&14=$15&16=$17&18=$19&20=$21&22=$23&24=$25&26=$27&28=$29&30=$31&
32=$33&34=$35&36=$37&38=$39&40=$41
[edited by: jdMorgan at 2:58 pm (utc) on June 22, 2007]
[edit reason] Fixed side-scroll [/edit]
The usual way to handle variable numbers of query-string name/value pairs is to write a rule for each case; Mod_rewrite is not a full-on scripting language, and it has limitations.
Jim
The structure of your rewriterule above is --to put it mildly-- non-optimal. What you're probably looking for si something like this:
RewriteRule ^([a-z0-9_-]+)\.php/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3&$4=$5&$6=$7&$8=$9 [NC,L]
RewriteRule ^([a-z0-9_-]+)\.php/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3&$4=$5&$6=$7 [NC,L]
RewriteRule ^([a-z0-9_-]+)\.php/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3&$4=$5 [NC,L]
RewriteRule ^([a-z0-9_-]+)\.php/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [NC,L]
And as stated previously, you can chain rules if more parameters need to be passed.
Jim
[edited by: jdMorgan at 3:15 pm (utc) on June 25, 2007]