Jump to content

Recommended Posts

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 :D)

Link to comment
https://forums.phpfreaks.com/topic/164867-some-mod_rewrite-questions/
Share on other sites

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

 

 

 

 

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]

  • 1 month later...

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?  :confused: please  :)

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

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.

bah....  :facewall: :facewall: :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: :shrug:

 

thanks for all your help anyway :)

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.