简体   繁体   中英

Restructure php url with slashes using htaccess

i already asked for help for the same problem same weeks ago but didnt really got a working solution, so i am doing another try to fix it.

So i have a url look like

website.com/proj1/post.php?id=130

but i want to restructure the url to look like

 website.com/proj1/post/130

And after discussion, a guy suggested to use following htaccess rules

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/proj1/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/proj1/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

He said that it worked in his environment, but i didnt manage to get work on xampp or my real server ?

can you confirm that the code works ? and do you have any idea why it might not work for me?

Just for the record, i have have placed the htaccess files in the folder "proj1", so it not under the root.

Your rewriteRule doesnt look right as you are using wrong backrefrences and RewriteConds. You can use the following rule in proj1/.htaccess

RewriteEngine on


RewriteRule ^post/([^/.]+)/?$ /proj1/post.php?id=$1 [L]

If you have a fixed path prefix of /proj1/post , it's enough to capture the number and use that number in the substitution

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

If both post and the number are variable, you must capture each part separately

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

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