vadim88 Posted September 27, 2008 Share Posted September 27, 2008 Hi, I was wondering how could i convert my long dirty links into nice friendly urls i have the following: http://www.127.0.0.1/php/?act=articles&alias=MY-ARTICLE http://www.127.0.0.1/php/index.php?act=articles&alias=MY-ARTICLE http://www.127.0.0.1/?act=articles&alias=MY-ARTICLE http://www.127.0.0.1/index.php?act=articles&alias=MY-ARTICLE http://www.127.0.0.1/php/?act=articles&id=7 http://www.127.0.0.1/php/index.php?act=articles&id=7 http://www.127.0.0.1/?act=articles&id=7 http://www.127.0.0.1/index.php?act=articles&id=7 http://127.0.0.1/php/?act=articles&alias=MY-ARTICLE http://127.0.0.1/php/index.php?act=articles&alias=MY-ARTICLE http://127.0.0.1/?act=articles&alias=MY-ARTICLE http://127.0.0.1/index.php?act=articles&alias=MY-ARTICLE Notice the changes, that the top ones use www and the lower ones don't. one uses the /php directory others don't, Some uses the index.php file and some don't, Some has numbers in the 'id' key and some text in the 'alias' key. How can i write rewrite rules that first it will do the same if the url has www or don't, Second it will work regardless if the index.php file exists in the url or not, it will know if the 'id' key is a number it will go to id={id} and if the alias is a string it will use alias='{alias}' and the end result will be: http://www.127.0.0.1/articles/MY-ARTICLE.htm http://www.127.0.0.1/articles/MY-ARTICLE.htm http://www.127.0.0.1/articles/MY-ARTICLE.htm http://www.127.0.0.1/articles/MY-ARTICLE.htm http://www.127.0.0.1/articles/7.htm http://www.127.0.0.1/articles/7.htm http://www.127.0.0.1/articles/7.htm http://www.127.0.0.1/articles/7.htm http://127.0.0.1/articles/MY-ARTICLE.htm http://127.0.0.1/articles/MY-ARTICLE.htm http://127.0.0.1/articles/MY-ARTICLE.htm http://127.0.0.1/articles/MY-ARTICLE.htm Would appreciate any help given (Willing to pay 25$ for a full rules that works) Quote Link to comment Share on other sites More sharing options...
wolfkettler Posted September 27, 2008 Share Posted September 27, 2008 Hi Check out this: http://www.google.co.uk/search?hl=en&rls=com.microsoft:en-gb:IE-Address&rlz=1I7GGLR_en-GB&q=url+rewriting&meta= Not all providers allow you to use this procedure on their servers, though. Wolf www.wolfkettler.co.uk Quote Link to comment Share on other sites More sharing options...
vadim88 Posted September 27, 2008 Author Share Posted September 27, 2008 I know, I have access and can use it, And i do already i just need someone to help Or write the right rewrite rules that will produce what i wrote above, I can google myself so that post wasn't very helpful. Like i said i can pay someone to help out or write it down via paypal. Thanks. Quote Link to comment Share on other sites More sharing options...
vadim88 Posted September 27, 2008 Author Share Posted September 27, 2008 If it's easier and someone knows then an alternative solution would be converting each _GET key into it's own directory so if i have this: index.php?act=articles&code=test&alias=MY-ARTICLE then it will be index/act/articles/code/test/alias/MY-ARTICLE That's a good alternative as well. Thanks. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 I got some of it. http://127.0.0.1/articles/{number}.htm (or html) will show you index.php?act=articles&id={number} http://127.0.0.1/articles/{string}.html (or html) will show you index.php?act=articles&alias={string} The www. part should also be simple. Quote Link to comment Share on other sites More sharing options...
vadim88 Posted September 27, 2008 Author Share Posted September 27, 2008 Great could you show me the code? Or you want me to first transfer some to your paypal account? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 The code used for that is simply: .htaccess file RewriteEngine on RewriteBase / RewriteRule ^articles/([0-9]+)\.html?$ index.php?act=articles&id=$1 RewriteRule ^articles/(.+)\.html?$ index.php?act=articles&alias=$1 Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 If you also want the 'act' to be variable (and e.g. only contain letters and numbers): RewriteEngine on RewriteBase / RewriteRule ^([A-Za-z0-9]+)/([0-9]+)\.html?$ index.php?act=$1&id=$2 RewriteRule ^([A-Za-z0-9]+)/(.+)\.html?$ index.php?act=$1&alias=$2 Quote Link to comment Share on other sites More sharing options...
vadim88 Posted September 27, 2008 Author Share Posted September 27, 2008 Thank you. Will this work regardless if there is a www in the url or not? and will this work without typing the index.php file name? Thanks again. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 And instead of allowing any string for the 'alias', it would be better to allow only certain chars (at least disallow a forward slash, as that seems confusing). So instead of (.+) in the code, e.g. ([^/]+) - anything not a slash. To answer your post: It's pretty difficult to try the www. thingy out on 127.0.0.1 (I've come to find). But this code below should remove www. from all requests (the way I like to do it): RewriteCond %{HTTP_HOST} !^example\.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://example.com/$1 [L,R] Just put the three lines right after the RewriteBase line. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 and will this work without typing the index.php file name? Yes. Quote Link to comment Share on other sites More sharing options...
vadim88 Posted September 27, 2008 Author Share Posted September 27, 2008 First i appreciate the help if you want i can transfer some cash via paypal to your account. How about i show you what i have done and if someone finds something wrong let me know please. ### REWRITES ### Options +FollowSymLinks RewriteEngine on ### Global Acts ### # Rewrite act/?/.htm|html into index.php?act=$1 anything but forwarding slash for the next rule to exists RewriteRule act/([^/]+)\.(htm|html)$ index.php?act=$1 # rewrite index/act/?/?/?.htm|html into index.php?act=$1&$2=$3 # rewirtes stuff like index/act/login/code/00.htm into index.php?act=login&code=00 RewriteRule act/(.*)/(.*)/(.*)\.(htm|html)$ index.php?act=$1&$2=$3 ### Articles ### # rewrite articles/{number}.htm|html into index.php?act=articles&code=article-by-id&id=$1 RewriteRule articles/([0-9]+)\.(htm|html)$ index.php?act=articles&code=article-by-id&id=$1 # rewrite articles/{string&numbers}.htm|html into index.php?act=articles&code=article-by-alias&alias=$1 RewriteRule articles/([A-Za-z0-9]+)\.(htm|html)$ index.php?act=articles&code=article-by-alias&alias=$1 And i want the www to exists if there is no www then to use www i dont want it to be 127.0.0.1 but www.127.0.0.1 Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 Your code seems fine. If it's possible to add www. to 127.0.0.1 (I can't get it to work, but may be my settings), the code below will work (from the official Apache URL rewriting guide): RewriteCond %{HTTP_HOST} !^www\.127\.0\.0\.1 [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://www.127.0.0.1/$1 [L,R] it should be added as the first (Cond)/Rule in the .htaccess file. Quote Link to comment Share on other sites More sharing options...
vadim88 Posted September 27, 2008 Author Share Posted September 27, 2008 Well even before i added those rules i tried accessing the site using www.127.... and it didn't work but i guess your right about it , i will use this code when i will upload it to my live website. Thanks again. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 27, 2008 Share Posted September 27, 2008 No problem, check your Inbox (here on PHP Freaks) I hope it works on your live server. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.