Hi,
I have a website called www.site.com
I have a product.php file and a categories.php file that generate the URL's and content dinamically.

In .htaccess I have the following rule.

RewriteRule (.*)_(.*)\.html product.php?category=$1&name=$2
RewriteRule (.*)\.html categories.php?category=$1

Everything is ok by now.

When I try to change the rewritting rule to

RewriteRule (.*)/(.*)\.html product.php?category=$1&name=$2
RewriteRule (.*)\.html categories.php?category=$1

The paths to the images are messed up and also the paths to the css.
In my products.php file I have lines of code like

<?php include('includes/config.inc.php') ?> or <?php include('includes/myaccount.inc.php'); ?>

These still don't work if I change the rewritting rule.
Also I have a functions.js file that contains ajax scripts that communicate with the .php files inside the includes folder.

How can I tell the apache server to treat the (.*)/(.*)\.html URL as (.*)_(.*)\.html or how can I fix this ?

Thanks,
nevergone

Recommended Answers

All 4 Replies

Hi,

The problem with your images, css and javascript will occur, because your .htaccess is rewriting every request that is sent to the server.

To fix this, add the following two lines before your rewrite rules:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]

They will check whether the request is for an actual file or directory, and if not proceed to rewrite the request using the rules that follow afterward.

R.

Hi,

The problem with your images, css and javascript will occur, because your .htaccess is rewriting every request that is sent to the server.

To fix this, add the following two lines before your rewrite rules:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]

They will check whether the request is for an actual file or directory, and if not proceed to rewrite the request using the rules that follow afterward.

R.

Hi,
I've detected another problem.
If I change

RewriteRule (.*)_(.*)\.html product.php?category=$1&name=$2 to RewriteRule (.*)/(.*)\.html product.php?category=$1&name=$2

Then any url like www.site.com/category1/product.html is threated as being a categories.php file,instead of being a product.php file ?
Any idea on how to fix this ?

I think the problem might be two fold.

Firstly, you may want to address your use of (.*) . This means match any character, any number of times... and this will include the / character. I would be inclined to specify a finite list of the characters you expect to be in the URL, e.g. A-Z, 0-9, etc.

Also, try adding the [L] flag to your rules. As seen below. This means that if the rule matches, it's the last rule that is checked.

RewriteRule (.*)/(.*)\.html product.php?category=$1&name=$2 [L]
RewriteRule (.*)\.html categories.php?category=$1 [L]

R.

I think the problem might be two fold.

Firstly, you may want to address your use of (.*) . This means match any character, any number of times... and this will include the / character. I would be inclined to specify a finite list of the characters you expect to be in the URL, e.g. A-Z, 0-9, etc.

Also, try adding the [L] flag to your rules. As seen below. This means that if the rule matches, it's the last rule that is checked.

RewriteRule (.*)/(.*)\.html product.php?category=$1&name=$2 [L]
RewriteRule (.*)\.html categories.php?category=$1 [L]

R.

Hi,
I've added this code to the website

RewriteRule ^([^/]+)/(.+)\.html$ model.php?cat=$1&model=$2 [L]
RewriteRule ^(.+)\.html$ listmodels.php?cat=$1 [L]

The pages are generated correctly now,but the images are still messed up.
I tried to declare a constant as the webroot and place it before images,css and js

<?php define('ROOT_WEB', 'http://www.site.com'); ?>

Well,it worked for the images and CSS,but the AJAX functionality is messed up now .
I have this code in my product.php file

<script type="text/javascript" src="functions.js"></script>

And inside this I have functions like

function updatecart() {
	$.post("includes/updatecart.inc.php");
	show('cart');
	showcartquantity();
}

function emptycart() {
	$("#exegcute").load("includes/do_emptycart.inc.php");
	show("cart");
	showcartquantity();
}

function removeproduct(id) {
		$.ajax({
	  type: 'POST',
	  url: "./includes/removeproduct.inc.php",
	  data: "id="+id,
	  success: function(msg){
     	$("#addremvcart"+id).html(msg).fadeIn('slow');
   	  }
	});
	setTimeout(showcartquantity(),1000);
}

... NONE OF THEM ARE FUNCTIONAL NOW,and they worked before ,I tried to add this code in .htaccess

RewriteCond %{REQUEST_URI} !(css|images)

and

RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]

before my rewrite rules but none of them worked

btw,is there anyway to tell apache to threat category/product.html as category-product.html(this type of page is 100% functional and AJAX works too) - but the url must still show category/product.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.