Nathan
Nathan

Reputation: 666

htaccess clean url rewrite images, css and javascript etc not showing

I have created a .htaccess file so I can use some SEO friendly and pretty looking URLs.

I'm wanting to convert: http://domain.com/page.php?id=200&name=this-is-a-title

To: http://domain.com/200/this-is-a-title

When I go to the latter URL, it does work but images, css, js etc do not show up. After some Googling I found a few ways on how to ignore paths to files that don't exist and ignoring specific directories. However, whenever I add these rules/conditions the URL rewrite no longer works and I just get a 404 error.

Here is my .htaccess file:

# Turn rewriting on
RewriteEngine On

# Allow any files or directories that exist to be displayed directly
#RewriteCond %{REQUEST_FILENAME} !-f 
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(inc|css|img|js)($|/) - [L]
#RewriteCond %{REQUEST_URI} "/css/" [OR]
#RewriteCond %{REQUEST_URI} "/img/"
#RewriteCond %{REQUEST_URI} !^/css

# Set rewrite rule
RewriteRule ^([0-9]+)/([^.]+)$ page.php?id=$1&name=$2

As you can see where the commented out lines are, I've tried a few rules to ignore the css/img/js folders, but none work.

Does anyone have any ideas why this might be happening?

EDIT:

Just for some more info, when I right-click and view image info on where an image should be, it's saying its path is 200/img/image.jpg and when I change the image's src to ../img/image,jpg it works, however, I can't really do this as I have some PHP include files that I can't edit because they are used in all my other pages on the site as well, so I can't really go and change the image src in them without duplicating the php file, which would seem silly having two of them

Upvotes: 0

Views: 4126

Answers (4)

Srinivas Batchu
Srinivas Batchu

Reputation: 902

I had the same problem. Then I used this simple trick and it works like a champ.

All we need to specify the Dynamic-base-path of these files like Images, Stylesheets, JavaScript files.

index.php :

<head>
<?php
     if(isset($_GET['base'])){
         echo "<base href='".$_GET['base']."' />";
     }
?>
...
...
</head>

.htaccess :

RewriteEngine On 

# Allow any files or directories that exist to be displayed directly

#RewriteCond %{REQUEST_FILENAME} !-f 
#RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

RewriteRule ^([^.]+)$ index.php?mode=$1
RewriteRule ^([^.]+)/([^.]+)$ index.php?mode=$1&param=$2&base=../

In the .htaccess file we have to specify the 'base' parameter manually based upon the number of parameters in the clean URL.

Upvotes: 0

anubhava
anubhava

Reputation: 785216

Just keep your rewrite rule as is but add a rule to fix links for css/js/images:

RewriteEngine On

# fix js/images/css
RewriteRule ^.+?/((img|css|js)/.+)$ /$1 [L,R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/([^.]+)/?$ page.php?id=$1&name=$2 [L,QSA]

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143896

You need to either change your links to absolute URLs (that start with a /) or add a relative URL base to the header of your pages:

<base href="/" />

Upvotes: 3

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

You shouldn't need all those rules, and most of them were commented out (#). Try:

# Turn rewriting on
RewriteEngine On

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

# Set rewrite rule
RewriteRule ^([0-9]+)/([^.]+)$ page.php?id=$1&name=$2

Upvotes: 1

Related Questions