0

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

4 Answers 4

3

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="/" />
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Unfortunately I had already tried those two conditions together. But they return a 404 error, which is why they are commented out. I just showed the comments so you could see what I have tried.
0

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]

5 Comments

I get a 500 Internal Server Error doing that :\
can you try updated rule. Make sure fix js/images/css is your first rule.
If you still get 500 then do check your Apache error.log for the reason why 500 is appearing.
I got another 500 error trying your above code. And in the error log I get a list of File does not exist: /home/fhlinux195/d/domain/user/htdocs/200/css/style.css for each image/css and then [Mon Jan 20 09:26:31 2014] [alert] [client 212.50.188.36] /home/fhlinux195/d/domain/user/htdocs/.htaccess: RewriteRule: cannot compile regular expression '^.+?/((?:img|css|js)/.+)$'\n
Very strange, try this: RewriteRule ^.+?/((img|css|js)/.+)$ /$1 [L,R=301,NC] and make sure it this is copy paste as is. You should not be getting '\n as per your log.
0

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.

Comments

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.