krike Posted July 5, 2009 Share Posted July 5, 2009 I have a few questions about rewriting url I have the following which works Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^tutorial/(.*+)$ view_tutorial.php?tutorial_id=$1 [L] RewriteRule ^home(.*+)$ /index.php [L,QSA] as you can see I redirect index.php to /home, but there are other pages I would like to do this (like advertise.php) I copy pasted the code and added this at the end of the file: RewriteRule ^advertise(.*+)$ /advertise.php [L,QSA] Question 1 but for that part I'm getting a 500 internal server error but not for index.php, can I just add another rewrite under the index.php? Question 2 ^tutorial/(.*+)$ view_tutorial.php?tutorial_id=$1 shows /tutorial/21 but I would like to display the tutorial name instead of the id, any idea how I would do this? or is there something else I should do? any help would be appreciated (noob here ) Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/ Share on other sites More sharing options...
wildteen88 Posted July 5, 2009 Share Posted July 5, 2009 If you're wanting to use site.com/home instead of site.com/index.php or site.com/adverstise instead of site.com/advertise.php etc Then you'd need to use only 1 single require rule RewriteRule ^(.*+)$ /$1.php [L,QSA][L] Question 2 ^tutorial/(.*+)$ view_tutorial.php?tutorial_id=$1 shows /tutorial/21 but I would like to display the tutorial name instead of the id, any idea how I would do this? or is there something else I should do? You will have to modify your script so it generates the links with the tutorial title contained within in it. Example code echo '<a href="'.$row['tutorial_id'].'-'.str_replace(' ', '_', strtolower($row['tutorial_title'])).'">'.$row['tutorial_title'].'</a>'; That code will produce a link like site.com/123-how_to_install_php Now for your rewriteRule you can use RewriteRule ^tutorial/([0-9]+)$ view_tutorial.php?tutorial_id=$1 Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-869393 Share on other sites More sharing options...
krike Posted July 6, 2009 Author Share Posted July 6, 2009 oooh I see thanks will try that out tonight Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-869743 Share on other sites More sharing options...
krike Posted July 6, 2009 Author Share Posted July 6, 2009 This is what I have and I get an internal server error (when removing last line I have no problems) Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^tutorial/([0-9]+)$ view_tutorial.php?tutorial_id=$1 [L] RewriteRule ^(.*+)$ /$1.php [L,QSA] Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-869911 Share on other sites More sharing options...
krike Posted August 16, 2009 Author Share Posted August 16, 2009 ok, this is what I have and it "sort of" works RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^tutorial/([0-9]+)$ view_tutorial.php?tutorial_id=$1 RewriteRule ^user/(.*)$ ./profile.php?username=$1 when I go to http://cmstutorials.org/tutorial/2 or http://cmstutorials.org/user/krike everything displays correctly but when I add a / at the end it won't work anymore. anyone know how to fix this? please Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899325 Share on other sites More sharing options...
roopurt18 Posted August 16, 2009 Share Posted August 16, 2009 Add the following before your dollar sign, which means "optionally may end with a forward slash but do not capture it": /? Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899357 Share on other sites More sharing options...
krike Posted August 16, 2009 Author Share Posted August 16, 2009 thanks, I did it but I get internal server error but I can still access the page without the / this is what I have RewriteRule ^(.*)/?$ /$1.php RewriteRule ^tutorial/([0-9]+)/?$ ./view_tutorial.php?tutorial_id=$1 RewriteRule ^submitted/([0-9]+)/?$ ./submitted_tutorial.php?tutorial_id=$1 RewriteRule ^add_favorite/([0-9]+)/?$ ./add_favorites.php?tutorial_id=$1 RewriteRule ^favorites/(.*)/?$ ./favorites.php?username=$1 RewriteRule ^user/(.*)/?$ ./profile.php?username=$1 RewriteRule ^edituser/(.*)/?$ ./edit_profile.php?username=$1 Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899473 Share on other sites More sharing options...
roopurt18 Posted August 16, 2009 Share Posted August 16, 2009 The following rule will catch everything, so it should probably be last: RewriteRule ^(.*)/?$ /$1.php I also like to append [QSA,L] to my rules (query string append, last), like so: RewriteRule ^tutorial/([0-9]+)/?$ ./view_tutorial.php?tutorial_id=$1 [QSA,L] Whenever I have rewrite problems there are two things I do to identify them. 1) I comment out all of the rewrite rules and test them one by one until I find the broken one. 2) If you check the mod_rewrite documentation, you can turn on rewrite logging and check your apache error log. Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899590 Share on other sites More sharing options...
krike Posted August 17, 2009 Author Share Posted August 17, 2009 bah.... :facewall: :facewall: this is what I have: SecFilterEngine Off SecFilterScanPOST Off RewriteEngine on #RewriteLog "/var/www/vhosts/xxxxxxxx/httpdocs/rewrite.log" #RewriteLogLevel 5 RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^tutorial/([0-9]+)$ ./view_tutorial.php?tutorial_id=$1 [QSA,L] RewriteRule ^submitted/([0-9]+)$ ./submitted_tutorial.php?tutorial_id=$1 [QSA,L] RewriteRule ^add_favorite/([0-9]+)$ ./add_favorites.php?tutorial_id=$1 [QSA,L] RewriteRule ^favorites/(.*)$ ./favorites.php?username=$1 [QSA,L] RewriteRule ^user/(.*)$ ./profile.php?username=$1 [QSA,L] RewriteRule ^edituser/(.*)$ ./edit_profile.php?username=$1 [QSA,L] RewriteRule ^(.*)/$ /$1.php I commented the rewritelog because....... guess what??..... it's causing an internal server error :'( should it be on top or something? The path is correct I'm sure, or should it be a txt file? :shrug: thanks for all your help anyway Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899875 Share on other sites More sharing options...
roopurt18 Posted August 17, 2009 Share Posted August 17, 2009 Try putting rewrite logging in the vhost configuration in httpd.conf I guess. Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899887 Share on other sites More sharing options...
krike Posted August 17, 2009 Author Share Posted August 17, 2009 can't access it, I'm on shared hosting. however I thought maybe I could edit all url's ending with / and remove it so if anyone goes to /contact/ it will change to /contact. but no idea on how to do that... Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-899894 Share on other sites More sharing options...
roopurt18 Posted August 17, 2009 Share Posted August 17, 2009 RewriteRule ^(.*)/$ /$1.php Your last rule is here is matching URLs ending in a forward slash. It's also trying to send them to a file off of the root directory of the server since you left out the . (current directory) symbol in the /$1.php Quote Link to comment https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/#findComment-900272 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.