Mobile app version of vmapp.org
Login or Join
Shanna517

: .htaccess rewrite URL leads to missing CSS I am trying to make my url from this: www.example.com/view.php?id=15 To this: www.example.com/watch/15 My code in .htaccess is as follows: Options

@Shanna517

Posted in: #Htaccess #RelativeUrls

I am trying to make my url from this:
example.com/view.php?id=15

To this:
example.com/watch/15

My code in .htaccess is as follows:

Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^watch/([^/]+) view.php?id= [NC]


Now if I go to example.com/watch/15 it loads the content but without any JS, CSS because in this case it looks for CSS and js under the /watch folder which doesn't exist.

How do I make this option work properly? I don't want to create watch folder and copy-paste CSS and JS so it will embed properly.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

Not being able to find your JavaScript and CSS files is a client-side/browser issue related to your URL-path, it's not something that should be fixed in .htaccess (at least not in this case) - although it is because you are changing this URL-path (in .htaccess) that you are experiencing this problem.

As closetnoc suggests in comments this problem is caused by using relative URLs in your HTML. Relative to what? If you changed your JavaScript and CSS URLs to be root-relative (starting with a slash) or even absolute then you would not get this problem.

Alternatively you can include a base element in the head section of your HTML document. This references the absolute URL that all relative URLs are relative to. In other words, since you are expecting these relative URLs to be relative to the document root then add the following to the head section:

<base href="http://example.com/">


Now, a relative URL such as styles/default.css referenced in a document at URL /watch/15 will request example.com/styles/default.css, not example.com/watch/styles/default.css.
Reference: developer.mozilla.org/en-US/docs/Web/HTML/Element/base

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme