Thusitha Wickramasinghe
Thusitha Wickramasinghe

Reputation: 1103

.htaccess resolve relative path issue

I'm new to .htaccess files. In my website i have page for users to search books by its category and sort by some parameters. real page url is like this. I have used few css and js files with relative paths. Ex: <link href="../css/mystyle.css" rel="stylesheet">

www.example.com/post/index.php?cat=science-fiction&sort=date

I have modified this url by using .htaccess file. for more meaningful url experience.

#Turn Rewrite Engine On
RewriteEngine on

#Rewrite for ads.php
RewriteRule ^([0-9a-zA-Z_-]+) index.php?cat=$1 [NC,L]

I wanted to have my urls like this. and Its working.

www.example.com/post/science-fiction

www.example.com/post/science-fiction?sort=date

But my problem is, when user enters "/" end of the url. Page cant load css or js files. I want to Redirect or Ignore "/" end of the Url.

Problem with this URL

www.example.com/post/science-fiction/

www.example.com/post/science-fiction/?sort=date

How to resolve this changing .htaccess file

Upvotes: 2

Views: 672

Answers (1)

anubhava
anubhava

Reputation: 785156

You can use these rules in your site root .htaccess:

RewriteEngine on

## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?cat=$1 [QSA,L]

For long term approach consider using absolute path in css like this:

<link href="/css/mystyle.css" rel="stylesheet">

Upvotes: 3

Related Questions