简体   繁体   中英

correct way to use mod_rewrite

I have followed lots of stackoverflow questions and Googling But i cant do my url rewrite correctly. Here is an simple exapmle of my website

root-|
     |__mydomain-|
                 |
                -|
                 |__cat1-|__.htaccess
                 |       |__index.php
                 |       |__single.php
                 |
                 |__.htaccess
                 |__index.php  // home-page

my home page contains A menu link: mysite.com/cat1
-> which is showing "root/cat1/ index.php " correctly.

Question1:
For any other links which contains cat1/post-id/title i need to rewrite url to single.php?id=post-id

mysite.com/cat1/123/title  
   ->  should show single php with content of post with id:123

What i wrote in root/cat1/.htaccess

Options +FollowSymLinks
RewriteEngine on
<IfModule mod_rewrite.c>

RewriteBase /cat1/


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ single.php?id=$1 [L,QSA]

</IfModule>

i get errors like

[Mon Nov 24 13:49:08.549284 2014] [:error] [pid 7144:tid 784] [client 127.0.0.1:21397]
script 'C:/wamp/www/single.php' not found or unable to stat

You can change your rule a bit inside /cat1/.htaccess :

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cat1/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+)/(.+?)/?$ single.php?id=$1&title=$2 [L,QSA]

</IfModule>

Make sure this is in /cat1/.htaccess

PS: I added title parameter so that you can verify title is correct (if needed) inside single.php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM