1

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!

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

7 Comments

this worked but all the css is gone. If I chance the path to make it work for this page, it will ruin all the other pages
For css/js just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
I have my css file in 'test/css/style.css' and the post.php file in 'test' folder, before the htaccess stuff, the css was linked with href="css/style.css" so what should I put now?
do you know how I can fix that?
As I commented above your css should have link as href="/test/css/style.css"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.