Forum Moderators: phranque

Message Too Old, No Replies

htaccess single directory rewrite

         

mcotten

9:15 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



My shopping cart has three parameters for screen, store code and category/product code. By default there are rewrite options for domain.com/category/categorycode and domain.com/product/productcode. I have a third-party module that handles the category or product parameter so it can format the URLS as: domain.com/code.html. However I would like to remove the extension completely for (domain.com/code). I have it working except the directoryindex goes to the Merchant2 folder instead of the root. Any help would be much appreciated.

DirectoryIndex merchant.mvc?Screen=SFNT&Store_Code=SSS&Category_Code=home

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !-s
RewriteRule ^([^/]*)$ /Merchant2/merchant.mvc?Screen=NBSEOLINK&thecode=$1 [L=301]

jdMorgan

4:24 am on Jul 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may or may not affect your problem, which I don't fully understand (being more verbose, using a narrative style, and specifying specific URLs and corresponding filepaths is often helpful.) However, "-s" should be checked on REQUEST_FILENAME, not REQUEST_URI, and "[L=301]" is invalid:

RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/]*)$ /Merchant2/merchant.mvc?Screen=NBSEOLINK&thecode=$1 [L]

Also, there is really no reason to check both "-f" and "-s" -- Pick one or the other, depending on whether you want to allow an empty physical file to inhibit the rule. See Apache mod_rewrite documentation, RewriteCond CondPattern description, for the differences between -s and -f. Since file-exists checks are system-resource-intensive (sometimes requiring a physical read of the disk itself, with resulting delay), you should make great effort to avoid doing them -- and especially to avoid doing them unnecessarily.

Jim

mcotten

6:59 am on Jul 21, 2009 (gmt 0)

10+ Year Member



Ok, thank you for clearing up those issues. I was trying to summarize the shopping cart has its default URL format and a provided rewrite format. I purchased a third-party module that further shortens the URL but I want to remove the file extension from those URLs. I can't provide the live URLs because it is a live store and my attempt is causing a error.

Original Default URL Format:

Storefront:
[domain.com...]
Category:
[domain.com...]
Product:
[domain.com...]

So even without rewrites I have to redirect the root to the storefront URL. I also have to include a category code in the storefront URL so I can assign products to be displayed on the homepage. So the actual full URL to my homepage is in the format of:
[domain.com...]

Provided URL Rewrite Format:

Storefront:
[domain.com...]
Category:
[domain.com...]
Product:
[domain.com...]

Generated .htaccess when rewrite settings enabled:

RewriteEngine On

RewriteRule ^Merchant2/admin.mvc? - [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^product/([^/.]+).html$ /Merchant2/merchant.mvc?Screen=PROD&Product_code=$1 [L]
RewriteRule ^category/([^/.]+).html$ /Merchant2/merchant.mvc?Screen=CTGY&Category_code=$1 [L]
RewriteRule ^([^/.]+).html$ /Merchant2/merchant.mvc?Screen=$1 [L]

Third-Party Module URL Format:

Category:
[domain.com...]
Product:
[domain.com...]

Third-Party Provided .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ /mm5/merchant.mvc?Screen=NBSEOLINK&thecode=$1 [L]

My Desired URL Format:

Storefront:
[domain.com...]
Category:
[domain.com...]
Product:
[domain.com...]

Current .htaccess:

DirectoryIndex /Merchant2/merchant.mvc?Screen=SFNT&Store_Code=storecode&Category_Code=categorycode
#
RewriteEngine On
#
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/]*)$ /Merchant2/merchant.mvc?Screen=NBSEOLINK&thecode=$1 [L]

However, the homepage rewrite doesn't work. It works for if I type the URL: [domain.com...] but I can't get it to work for the root of the domain.

jdMorgan

3:25 pm on Jul 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



However, the homepage rewrite doesn't work. It works for if I type the URL: [domain.com...] but I can't get it to work for the root of the domain.

Please define "doesn't work" -- Server error? Nothing happens? The wrong thing happens?
Please define "root of the domain" -- Do you mean "www.domain.com/"?

Also, please let us know *where* each "generated .htaccess file" is located -- The function of code in .htaccess files depends on that file's location.

Not trying to be pedantic, but we can't use imprecise language here unless you want incorrect answers... It is best to assume that we know nothing whatosever about your software, your URL-space, or your file locations (which is why I asked for both URLs and the filepaths that those URLs resolve to).

Also, our contributors here are limited both in number and in willingness to invest their time; Clear questions get faster, better answers.

So that perhaps you won't consider this latest exchange to be a total loss, be aware that if "the root of the domain" that you want to rewrite is indeed "www.domain.com" and if this .htaccess code is located in /Merchant2/.htaccess, then it cannot "work", because only URLs starting with /Merchant2 will cause the .htaccess code in that subdirectory to be executed. If you want to rewrite the "home page" URL, then you'll have to put a rule in www.domain.com/.htaccess

Thanks,
Jim

mcotten

5:31 pm on Jul 21, 2009 (gmt 0)

10+ Year Member



Apologies. I tried to be as thorough as I could in my last post. This has been an ongoing issue for some time that I’ve spent a lot of time trying to resolve so I am very appreciative. I know HTML well enough but these Apache rewrites are difficult for me.

Yes, by root of the domain I meant www.domain.com/
My .htaccess file is located www.domain.com/
The shopping cart is located www.domain.com/Merchant2/

After uploading the .htaccess file, www.domain.com/ returns a fatal error generated from the shopping cart “Unknown page error”. Which sounds like the shopping cart is not receiving the Screen parameter. But www.domain.com/productcode and www.domain.com/categorycode work and www.domain.com/Merchant2 returns the storefront page. I cannot get the path for the DirectoryIndex to properly resolve the storefront.

jdMorgan

10:34 pm on Jul 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can probably remove the DirectoryIndex directive, and add this rule:

RewriteRule ^$ merchant.mvc?Screen=SFNT&Store_Code=SSS&Category_Code=home [L]

Note that this rule must precede any other internal rewrite rules which check for "file exists" in any way.

Also note that the vendor-supplied code fails to escape literal periods. For example,


RewriteRule ^product/([^/.]+).html$ /Merchant2/merchant.mvc?Screen=PROD&Product_code=$1 [L]

should be

RewriteRule ^product/([^/.]+[b])\.h[/b]tml$ /Merchant2/merchant.mvc?Screen=PROD&Product_code=$1 [L]

Jim

mcotten

9:23 pm on Jul 27, 2009 (gmt 0)

10+ Year Member



Thanks very much Jim. You da man.