Hi, I'm working on an ecommerce site right now that implements php and I'd prefer to use static pages for the category and product pages. The site has over 100 categories and 800+ products however where the urls are all generated dynamically through the database. I don't feel like manually renaming each page from dynamic to static. After a bit of research I've come across some coding Mod Rewrite that can be implemented through .htaccess that will do this for me - is this correct? My current pages look like this: Categories: 'http://www.site.com/categories.php?cat=116' I would like it to use the category name, so 'site.com/example-category-name.php' Product Pages: http://www.site.com/proddetail.php?prod=1234567 I would prefer it to say 'site.com/example-product.php' where example-product is the name of the product, and not the product id. I'm not very familiar with this, so if someone could give me some help I'd greatly appreciate it, thank you.
What part do you need help with, the rewrite rules or modifying the code of your PHP script to get categories/products by name instead of ID (probably a few changes to DB queries)?
well it can be done with a post pend file, but that requires you to find out what the urls are, and replace them after the page has been created (with code in the post pend file), it is tricky, but wouldn't require direct modification of the application, but you'd still need to use mod rewrite.
Ok, I've done a little more research and think I have figured everything else out except how to properly form the rewrite rule in htaccess. My current, dynamically formed url example: 'site.com/proddetail.php?prod=test123' test123 being the product ID, I want the url to form using the product name I've assigned to test123, not the ID. For this example, product id: 'test123' is product name: 'Test Product' What I want: 'site.com/test-product.php' I've gone through dozens of sites trying to properly configure this, but haven't found any examples going from a dynamic url of product id to static url of product name. Is this even possible or do I have to use the product id? My database name for the product name is 'pName' if that is needed. I'm grateful for any and all input, thank you.
Think of the problem the other way around because, essentially it's a find and replace. Your public URL will be... site.com/test-product-123.php But, internally, on your server, you want to treat it as if it were... site.com/proddetail.php?prod=test123 So, the code is... RewriteEngine on RewriteRule ^test-product-([0-9]+).php proddetail.php?prod=test$1 This is basically saying find "test-product-([0-9]+).php" and replace it with "proddetail.php?prod=test$1" The bit in brackets is stuffed into $1 You need to play around with it a bit to get it to match the exact naming convention you want to use for your static pages. Also, in your question, it's not clear whether you want to use genuine static pages, or whether you're just trying to make your pages appear static? I suspect the latter. Which is more commonly known as "Search Engine Friendly URLS".
Thanks for the reply, I appreciate it. For some reason the forum isn't notifying me of replies to my thread subscriptions. Anyways, yes you're correct. I'm going more for the Search Engine Friendly URLs than actual static pages. I have configured htaccess with: Options +FollowSymLinks RewriteEngine on RewriteRule ^/?([A-Za-z0-9]+)(/)?$ /proddetail.php?prod=$1 [L] Code (markup): which allows my products to be reached by typing the url 'http://www.site.com/test123' and I'm happy with that. However, the same product is still reachable at 'http://www.site.com/proddetail.php?prod=test123', so I would like to create a 301 redirect that way if someone either types in '/proddetail.php?prod=test123' or comes across it from an old google spidered url, it will redirect them to 'site.com/test123' instead. I don't want duplicate urls of the same product, and I don't want to write a redirect in htaccess for each product, either. I am having trouble setting up the redirect though, and so far have either made it loop endlessly or redirect to my homepage. This issue is driving me crazy, so if someone would please tell me how to make this: http://www.site.com/proddetail.php?prod=test123 Code (markup): redirect to this: http://www.site.com/test123 Code (markup): I would be forever grateful. This is what I have in htaccess which creates the shorter, Search Engine Friendly URL example above: Options +FollowSymLinks RewriteEngine on RewriteRule ^/?([A-Za-z0-9]+)(/)?$ /proddetail.php?prod=$1 [L] Code (markup): Thanks!
That's a very interesting problem! The mod rewrite occurs well before the request gets to the PHP layer, so I'm not sure if the PHP ever knows what the original (un-modrewritten) url was. And clearly you're going to get eternal loops quite easily if you start redirecting back to the other form. Unless, the only thing I can think of, is to put an extra paramter on if it was rewritten by mod-rewrite. So, if you had... RewriteRule ^/?([A-Za-z0-9]+)(/)?$ /proddetail.php?prod=$1&rewrite=true You could test if $_GET['rewrite']=='true' and if it wasn't, you'd know that someone had used your old url?
Hello, My problem is that my rewrite is not allowing the Get search query comand to work. The correct page is coming up, but the search query is missing. Could you tell me what to do to fix this? Thanks
Yes: Options +Indexes Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^news/(.*)\.shtml news.php?query=$1 So, /news/football.shtml goes to news.php , but with no query.
There's nothing wrong with your rewrite rule - i ran it thru a regular expression tester and it works fine. So, try it without the RewriteCond's and see if it works then?
It made no difference. I think this rewrite is interferring with it: RewriteRule ^(.*)\.shtml search.php?query=$1&command=search
No, I just tried it without that rewrite and it still will not work. Here is my Get command: $searchquery = ($_GET['query']);
Does it all work ok if you manually type the address with &query= on the end? If not, the problem is in your PHP not your htaccess, so post your code
No, it does not take the query. Here's the code for taking the query: $searchquery = urlencode($_GET['query']); $XMLFILE = "http://news.google.com/news?h1=en&ned=us&q=$searchquery&ie=UTF-8&output=rss"; $TEMPLATE = "http://mysite.org/newsfeedtemplate.html"; include("rss2html.php");
I'm not sure, but I think you may be misunderstanding me? The htaccess variable you provided in the above example redirects 'site.com/testabc' to 'site.com/prodddetail.php?prod=testabc'. I want it to do the exact opposite 'site.com/prodddetail.php?prod=testabc' redirect to 'site.com/testabc'. I was under the impression that this was possible...am I wrong? Thanks.
Well, my advice is that you're taking too big a step here without testing things at each stage. To me it looks like... 1) You're using a mod rewrite to rewrite an address and turn it into the form ?query=theQuery 2) You then retrieve that in PHP 3) You build it into a url 4) You send that url thru presumably an RSS reader 5) You parse the results And somewhere in that lot, something goes wrong! You assume its the modrewrite, but it could be anything. So, start breaking it down bit by bit. Comment out all the RSS reading bits, and just echo $searchquery. Does it come out on the screen? If so, the GET is fine. Then try it using the modrewrite, still fine? Then the problem isn't there and you're looking at the bit where you build the RSS address, so comment that bit back in and echo it to the screen - does it look right? Does it work if you paste what you have generated into the address bar?
add... echo "Search Query > ".$searchQuery." <"; and... echo "XMLFILE > ".$XMLFILE." <"; after the search query and xml file lines above and see what comes out on the screen
you can't make a page static using mod rewrite. Mod Rewrite is used to rewrite URLS it has nothing to do with pages or code