0

I have coded a PHP website on wamp server, in that I have used .htaccess file to rewrite the URL.

My .htaccess is like,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test.*$ test.php?id=

It does rewriting the URL like http://localhost/test/?id=5 to http://localhost/test/5

The issue is when I enter http://localhost/test it is loading the css correctly, but if I write http://localhost/test/ the css is blocked.

I have checked the que : .htaccess does not load css and js

but I can not figure it out why this happening. where I am wrong?

2 Answers 2

2

First, your code should look like this

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)$ /test.php?id=$1 [L]

MultiViews is important here, because your symlink and file have the same base name.
This way, http://localhost/test won't be matched anymore (because this is not what you want). Neither http://localhost/test/, only http://localhost/test/NUMBER (without a trailing slash)

Then, your css (and images, and javascript, etc) problem is because of virtual directory generated by your rule. In other words, it is trying to resolve your css/images/js in /test/ directory right now.

To avoid that behaviour, add this code in your file test.php, right after <head> html tag:
<base href="http://localhost/" />

Also, your css (and js, images, etc) html links must not begin with a slash /

Example for a css stylesheet:
<link rel="stylesheet" href="css/style.css" type="text/css" />
-----------------------------------------------^ No slash here

Sign up to request clarification or add additional context in comments.

2 Comments

I want to approve this answer too!! but stack overflow is not allowing me.. Thanks for your effort!
You're welcome. But my answer provides you the solution when using virtual directories. You may want to use that, instead of avoiding it. That's up to you
1

use this :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test*$ test.php?id=

editted :

just remove .(dot) on ^test.*$

4 Comments

just remove . on your ^test.*$
Tried.. but still it is not loading the css, if I view the page source and when i click on css link it is not showing the source...
can you show the css path when using slash user/ and just using user ?
I got the issue. it is because I have put a directory in my local host. after putting all the content in the www it is working cool!! ;)

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.