canadabeeau Posted November 6, 2009 Share Posted November 6, 2009 Hi I was wandering if anyone has ever implemented URLs on their PHP run website using URLS like wikipedia does (i.e. http://en.wikipedia.org/wiki/Moodle) If so can you please help me learn how to setup the code for IIS/Apache so I can do this ASAP Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/ Share on other sites More sharing options...
Bricktop Posted November 6, 2009 Share Posted November 6, 2009 Hi rhodry_korb, You need to investigate mod_rewrite, have a look at this article which explains the process. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952474 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 Thanks Bricktop, but when I install Joomla say on my server (IIS) I dont need to do any URL rewriet is this becuase it uses index.php/about-us sort of format? I am a bit confused as to why I need mod_rewrite, would I need this for the Joomla example or only the wikipedia example? Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952476 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 If someone could maybe submit their code as well, it would be much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952480 Share on other sites More sharing options...
Bricktop Posted November 6, 2009 Share Posted November 6, 2009 Hi rhody_korb, I assume Joomla already makes use of mod_rewrite for the rewriting of all it's URL's, this is why you don't use it when using Joomla. If you want to write your own PHP website, and use the URL format you require, you will need to use mod_rewrite. The tutorial I linked to above includes code snippets. Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952481 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 Okay thankyou, I will attempt this, wish me luck Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952482 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 Any ideas how to get it to work so it is not http://www.best-food-of-the-usa.com/arizona/mesa/top10.html but more like http://www.best-food-of-the-usa.com/arizona/mesa/top10, notice no .html or .php etc Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952491 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 I really do require the layout to be like wikipedia http://en.wikipedia.org/wiki/Moodle or in my case http://www.domainofmine.com/aboutus which is really like http://www.domainofmine.com/index.php?uri=aboutus (my current way, but looks messy) or http://www.domainofmine.com/index.php/aboutus.html (http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/) Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952494 Share on other sites More sharing options...
Bricktop Posted November 6, 2009 Share Posted November 6, 2009 Hi rhodry_korb, The tutorial I posted has all the information you need to start off with mod_rewrite, but I'll post some code for you. Firstly, you need to create an .htaccess file, and paste the following code into it: ################################################### # Turn the RewriteEngine on. # ################################################### RewriteEngine on ################################################### # Add a leading www to domain if one is missing. # ################################################### # If this rule is used, the rewriting stops here # # and then restarts from the beginning with the # # new URL # ################################################### RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] ################################################### # Do not process images or CSS files further # ################################################### # No more processing occurs if this rule is # # successful # ################################################### RewriteRule \.(css|jpe?g|gif|png)$ - [L] ################################################### # Add a trailing slash if needed # ################################################### # If this rule is used, the rewriting stops here # # and then restarts from the beginning with the # # new URL # ################################################### RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$ RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L] ################################################### # Rewrite web pages to one master page # ################################################### # /somepage/ => master.php # # ?page=somepage # # /somesection/somepage => master.php # # ?section=somesection # # &page=somepage # # /somesection/somesub/somepage/ # # => master.php # # ?section=somesection # # &subsection=somesub # # &page=somepage # ################################################### # Variables are accessed in PHP using # # $_GET['section'], $_GET['subsection'] and # # $_GET['page'] # ################################################### # No more processing occurs if any of these rules # # are successful # ################################################### RewriteRule ^aboutus/ /index.php?uri=aboutus [L] RewriteRule ^contactus/ /index.php?uri=contactus [L] RewriteRule ^anotherpage1/ /index.php?uri=anotherpage1 [L] RewriteRule ^you-can-use-hyphens/ /index.php?uri=toseparatewords [L] Now save the .htaccess file and upload it to the directory your index.php file is in. You can now navigate to http://www.domainofmine.com/aboutus and http://www.domainofmine.com/index.php?uri=aboutus will load. The above code is fully commented so should be easy to follow. Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952505 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 Thankyou so much Bricktop, I had the mod_rewite goin just couldnt get the last part (wikipedia style) to work. Thanks heaps Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952506 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 I am sure wikipedia would not write out RewriteRule ^aboutus/ /index.php?uri=aboutus [L] RewriteRule ^contactus/ /index.php?uri=contactus [L] RewriteRule ^anotherpage1/ /index.php?uri=anotherpage1 [L] RewriteRule ^you-can-use-hyphens/ /index.php?uri=toseparatewords [L] into their .htaccess, thankyou bricktop but does anyone know another way that I don't have to do the above to acieve the wikipedia style URL Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952514 Share on other sites More sharing options...
keldorn Posted November 6, 2009 Share Posted November 6, 2009 Wikipedia is probably using a Regular expression. Like RewriteRule ^wiki/([a-zA-Z]+)$ /wiki.php?id=$1 Where $1 is everything in the parenthesis. Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952515 Share on other sites More sharing options...
Bricktop Posted November 6, 2009 Share Posted November 6, 2009 Hi rhodry_korb, You're right in that they wouldn't write everthing out like that, this is why you need to read the tutorials. Dynamic URL rewriting happens because the PHP application is written in that way. For example, the following Rewrite rule: RewriteRule ^news/category/([^/\.]+)/?$ /news.php?f=shownews&category=$1 [L] Will dynamically rewrite any news URL based on its category. So the one rule will allow for any number of news categories such as: http://www.domain.com/news/category/headlines http://www.domain.com/news/category/world http://www.domain.com/news/category/weather http://www.domain.com/news/category/sport You need to read up on mod_rewrite and look at how it functions, it's an extremely powerful and complex tool, the code I supplied you is merely a very basic mod_rewrite usage. Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952519 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 so how would i set $1 RewriteRule ^wiki/([a-zA-Z]+)$ /wiki.php?id=$1 for my situation where http://en.wikipedia.org/wiki/Moodle or in my case http://www.domainofmine.com/aboutus which is really like http://www.domainofmine.com/index.php?uri=aboutus (my current way, but looks messy) or http://www.domainofmine.com/index.php/aboutus.html (http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/) could you please help once more keldorn or someone else Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952521 Share on other sites More sharing options...
keldorn Posted November 6, 2009 Share Posted November 6, 2009 You probably will want to create an imaginary folder that doesn't exist, so decide on how you want your urls. For example you could haave example.com/company/about The reason why you will want it like that is so that your mod_rewrite doesn't interfere with your real folders. So say you have an index.php file that take 3 pages, ?page=about ?page=contact page?privacy You would use this regex in your mod rewrite. RewriteRule ^company/([a-zA-Z]+)$ /index.php?page=$1 These urls now would rewrite to your index..or whatever file. example.com/company/contact example.com/company/about example.com/company/privacy There are more things you can do with different regurarl expressions, that just matches letters. You could match numbers also. Some people prefer to match the numbers instead, where that number correlate to a auto-incrmenting id in their mysql table, and the words in the url are just for show and don't anything. An example of that would something like example.com/68/hello-world.html . The mod_rewrite would ignore the everything except the number and transfer that to the script like article.php?id=68 I hope this all makes sense becuase I cannot explain any more than that. Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952525 Share on other sites More sharing options...
canadabeeau Posted November 6, 2009 Author Share Posted November 6, 2009 Thankyou to Bricktop and keldorn for helping me, thankyou so much, I;m going to do some more exploring now and stop clogging the forum :-) Quote Link to comment https://forums.phpfreaks.com/topic/180540-solved-clean-urls/#findComment-952531 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.