php - .htaccess Multiple Rewrite Rules / Prioritizing -



php - .htaccess Multiple Rewrite Rules / Prioritizing -

here current htaccess file:

rewriteengine on rewritecond %{http_host} !^www\.website\.com [nc] rewritecond %{http_host} ([^.]+)\.website\.com [nc] rewriterule ^(.*)$ http://www.website.com/gotourl.php?urlid=?%1 [l] rewriterule ^(\w+)$ ./gotourl.php?urlid=$1 [nc,l] rewriterule ^(\w+)/$ ./gotourl.php?urlid=$1 [nc,l]

the website in question url shortener i'm working on testing/developing , personal use. above code may not ideal i'm not familiar .htaccess , made via googling , testing. want url shortening aspect:

http://website.com/1234 -> http://website.com/gotourl.php?urlid=1234 http://website.com/1234/ -> http://website.com/gotourl.php?urlid=1234

the problem cannot figure out how ignore directories. want specific directories go specific pages:

http://website.com/img (or) http://website.com/img/ -> http://website.com/img.php http://website.com/img/1234 (or) http://website.com/img/1234/ -> http://website.com/viewimage.php?imgid=1234

the main problem don't know how , ways i've tried rewrite to:

http://website.com/gotourl.php?urlid=xxxx

or end in infinite loop of redirects instead of going specific page i'm wanting. help appreciated.

in essence i'm looking help writing htaccess following:

url user visits -> url user sees http://website.com/img -> http://website.com/img.php http://website.com/img/xxxx -> http://website.com/viewimage.php?imgid=xxxx http://website.com/xxxx -> http://website.com/gotourl.php?urlid=xxxx

all paths should work or without trailing slash.

looking help maybe bit of explanation each line means can larn it.

the main thing want change, making rule more specific. shortened url can not contain slash or dot, alter both url shortening url's to: rewriterule ^([^/\.]+)/?$ gotourl.php?urlid=$1 [nc,l]. you'll need prevent 'gotourl.php' matching though. rule img lone more specific , should done first. rule img/something little less specific , can anywhere.

rewriteengine on rewriterule ^img/?$ img.php [l] rewriterule ^img/([^/]+)/?$ viewimage.php?imgid=$1 [l] rewritecond %{http_host} !^www\.website\.com [nc] rewritecond %{http_host} ([^.]+)\.website\.com [nc] rewriterule ^(.*)$ http://www.website.com/gotourl.php?urlid=%1 [l] rewriterule ^([^/\.]+)/?$ gotourl.php?urlid=$1 [nc,l]

php .htaccess mod-rewrite url-rewriting

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -