skip to Main Content

I have the following php file:

http://example.com/details.php?item=asdf

However, I would like the url to be more concise:

http://example.com/item/asdf/details

I tried using the the .htaccess file like so:

RewriteEngine on
RewriteRule ^item/(w+)/details$ details.php?item=$1

And the url rewrite works.

However, when I try to load main.css which resides at http://example.com from details.php, I get the following message from Chrome:

Failed to load resource: the server responded with a status of 404 (Not Found)

since it couldn’t find http://example.com/asdf/main.css.

Is there any way to use the original url’s “base” or “root” directory with the rewritten url? I would like to access main.css from a dynamically generated asdf “directory”. I don’t want to use the base tag in html because that seems rather messy, or a point to the parent directory since I’m using a template page.

I’ve tried RewriteBase / but that just creates another error. Can someone help me out?

2

Answers


  1. Chosen as BEST ANSWER

    I decided to go the <base> route, but instead of hard coding the url, I used php:

    <base href="/<?=$_SERVER["HTTP_HOST"]?>">
    

  2. Try with below if file not exists it will rewrite if exists it should work as default.

    RewriteEngine on
    RewriteBase /item/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^item/(w+)/details$ details.php?item=$1 [L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search