简体   繁体   中英

htaccess redirect query is not loading page

I cannot get this to work. I want

http://localhost/test/post.php?id=15 

to show

http://localhost/test/post/15

My attempt loads a page without css on it and shows me my customr 404 error page

Options -Indexes -MultiViews +FollowSymlinks
RewriteEngine On
RewriteBase /test/
ErrorDocument 404 /errors/404.php

RewriteCond %{QUERY_STRING} ^id=(.+)$ [NC]
RewriteRule ^post\.php post/%1? [R,L]

please help me out!

You will need to %{THE_REQUEST} variable for this. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1 .

Your code will be:

ErrorDocument 404 /errors/404.php
Options -Indexes -MultiViews +FollowSymlinks
RewriteEngine On
RewriteBase /test/

RewriteCond %{THE_REQUEST} /post\.php\?id=([^\s&]+) [NC]
RewriteRule ^ post/%1? [R=302,L]

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

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