rajchahal Posted July 18, 2008 Share Posted July 18, 2008 hi Could someone help with friendly url's pls. I already have a index.php page in the root and also a dynamic page called page.php - This page pulls database pages i.e contacts, cinema and so on. (using php5). If it's any easier I can move pages into folders and re-name to index.php ? My current url is www.mysite.co.uk/page.php?page=cinema and would like (prefer) www.mysite.co.uk/cinema or www.mysite.co.uk/page/cinema Thanks Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/ Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Your in luck. I recently had to find a solution to this problem. Because I too prefer those sorts of URLs. You need to make sure that your host or server has the "rewrite_module" enabled. If your running WAMP just click on the status bar icon, go to Apache, then go to Apache modules. Scroll down and look for rewrite_module, enable it if you haven't already done so... Then, create a file called .htaccess In that file put this: RewriteEngine on RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?page=$1§ion=$2 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ page.php?page=$1§ion=$2&subsection=$3 [L] The three different things allow for subsections etc etc... Hope that clears a few things up. This creates pages like www.mysite.com/aboutus/ Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-593236 Share on other sites More sharing options...
rajchahal Posted July 18, 2008 Author Share Posted July 18, 2008 hi ProjectFear This worked first time - spot-on. It works with all directories. I also have other pages in the whats on section current URL is: http://www.mysite.co.uk/whats_on_article.php?id=22 - any suggestions on this ? maybe 'whats_on_article/22 ? and http://www.mysite.co.uk/press.php - how can I remove .php? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-593247 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Just add another: RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L] But make it: RewriteRule /whatson/^([^/\.]+)/?$ whats_on_article.php?id=$1 [L] And for the other one: RewriteRule ^/press/$ press.php [L] Hope thats all good, I'm still not brilliant with mod_rewrite yet. EDIT: Here is a link that may help you. http://www.easymodrewrite.com/ Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-593258 Share on other sites More sharing options...
rajchahal Posted July 18, 2008 Author Share Posted July 18, 2008 thanks the last two commands don't work. my .htaccess is RewriteEngine on RewriteRule ^([^/\.]+)/?$ page.php?page=$1 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?page=$1§ion=$2 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ page.php?page=$1§ion=$2&subsection=$3 [L] RewriteRule /whatson/^([^/\.]+)/?$ whats_on_article.php?id=$1 [L] RewriteRule ^/press/$ press.php [L] so if I go to url www.mysite.co.uk/press or www.mysite.co.uk/whatson I'm actually getting the wrong page (page.php) I've also notices if I type in a page that does not exist i.e www.mysite.co.uk/doesnotexist I'm also being served up page.php rather than a page not found error.. Any clues pls Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-593276 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Try moving the bottom two rules to the top... In your page.php, have something like this: if(!isset($_GET['page'])){ $page = "home"; }else{ $page = $_GET['page']; } if(file_exists("pages/".$page.".php")){ //the file is valid, include it }else{ //404 error, include your 404 script. } Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-593362 Share on other sites More sharing options...
rajchahal Posted July 21, 2008 Author Share Posted July 21, 2008 hi again - sorry to keep this thread going - I've tried swapping the two lines to the top but still it does't work. then I tried to alter RewriteRule ^/press/$ press.php [L] to RewriteRule ^press$ press.php [L] I've removed forward slashes and now mysite.com/press works. I've also added this: RewriteRule ^whatson$ whats_on_listing.php [L] but the whats_on_listing.php i'm passing variables to on some occations, these are : whats_on_listing.php?viewstate=today whats_on_listing.php?viewstate=week whats_on_listing.php?viewstate=soon I want to achieve whatson/today whatson/week whatson/soon Thanks Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-595365 Share on other sites More sharing options...
JasonLewis Posted July 21, 2008 Share Posted July 21, 2008 Okay... Try this for the whats on thing: RewriteRule ^whatson/([^/\.]+)/?$ whats_on_listing.php?viewstate=$1&nextvariable=$2 [L] To add more variables, you do this, I think: () RewriteRule ^whatson/([^/\.]+)/([^/\.]+)/?$ whats_on_listing.php?viewstate=$1 [L] And so on... Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-595450 Share on other sites More sharing options...
rajchahal Posted July 21, 2008 Author Share Posted July 21, 2008 Hi I think it was working all along (most of it anyway)- The problem was that when I was trying it, I was being served a page without css and assumed it wasn't working at all. Whats seems to be happening is that when I add RewriteRule ^whatson/([^/\.]+)/?$ whats_on_listing.php?viewstate=$1&nextvariable=$2 [L] to the htaccess it also alters the root folder path. Rather than the root being www.mysite.com it changes it to www.mysite.com/whats_on_listing.php so the stylesheet and images cant be found. I added ../ infront of the css file and it seems to now work, though a bit tedious. Is there something i could add in htaccess to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-595463 Share on other sites More sharing options...
JasonLewis Posted July 22, 2008 Share Posted July 22, 2008 Yeah I have encountered this problem. Which is why I add full paths or links to my images/css files now. There may be a solution though, I think you can do some stuff with mod_rewrite but I'm happy with giving it full paths or links. Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-596242 Share on other sites More sharing options...
rajchahal Posted July 22, 2008 Author Share Posted July 22, 2008 hi again I've got the urls to work - thanks so much I'm now looking at sending a 404 error if someone goes to www.mysite.com/nopage you gave me the code: if(file_exists("pages/".$page.".php")){ //the file is valid, include it }else{ //404 error, include your 404 script. } The page.php is in the root, so I removed from the path 'pages/' But I keep getting the 404 error Is it possible to check the existance of a dynamic page in this way? as only page.php actually exists? I'm echoing the $page variable and this gives me the correct page name. Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-596428 Share on other sites More sharing options...
JasonLewis Posted July 22, 2008 Share Posted July 22, 2008 I've heard of this, but never tried it. At the end of your .htaccess place this: ErrorDocument 404 /path/to/404/error.php Where /path/to/404/error.php is the path to your 404 error page. Hope that works. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/115394-friendly-url-mod_rewrite/#findComment-596514 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.