Jump to content

General Guide to .htaccess Rate Topic   - - - - -

 
  • Brandito
  • Member
  • Members
  • Join Date: 17-Dec 06
  • 82 posts

Posted 12 February 2007 - 05:57 AM #1

This is just a copy of a post I made on another forum. I need to edit it some to make it more relevant to the needs of this forum/software. But I thought I would go ahead and post it.


Overview

I don't know if anyone here is familiar with Regex or .htaccess, since this forum seems to be based more around designing... But I thought I would share a few good tips that I know. Just an FYI, all the coding and tips in this tutorial will be placed in your .htaccess file. These rules will only work when hosting on a Apache web server. And will not work on a IIS web server. And speaking of that, if you are on a Windows, IIS server I would strongly recommend switching to a LAMP setup. For lots of reasons, which I won't go into detail about right now because it is a whole topic in itself.

Getting Started

To begin, open your favorite text editor. Like notepad, dreamweaver ect. Make a plain text file, and add this code:

Options +FollowSymLinks

RewriteEngine On

This is the base for all the coding that will follow. In some cases, like old versions of Apache, the first line might cause server problems. To comment it out, just add a '#' at the beginning of the line.

Custom Error Pages

No more stupid error pages... With this code you will be able to make error pages that match the look 'n feel of your website. To do so, just add this code:

ErrorDocument 403 /403.php

ErrorDocument 404 /404.php

Note: If you are going to make custom error pages, you won't want to have them indexed. So you will want to add these 2 meta tags to keep the page from being indexed.

<meta name="robots" content="noindex, follow" /> 

<meta name="GOOGLEBOT" content="noindex, follow" />

Blocking Access to Files

Sometimes, well many times actually it is a good idea to block access to certain files. Like config, or installation files. It is very easy to do. Use this code:

<Files filename.extention>

order allow,deny

deny from all

</Files>

Or if you would like to block access to a specific file type, add this code:

<Files *.extention> 

order allow,deny

deny from all

</Files>

Make Your Site Only Accessible With WWW

This is most usefull for e-commerce sites, because when you install a SSL it is only valid for the version of the domain it was issued to IE either the www, or the non-www version. But it is good for all websites to avoid double indexing, and therefore killing the SEO of that page. To make your website only accessible with WWW, use this code:

RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]

RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]

RewriteRule ^$ http://www.website.com/index.php [L,R=301]

Note: People can still access your website by typing in website.com, but it will just rewrite (redirect) them to http://www.website.com/index.php and will always force the WWW in the URL. You can also of course, change 'index.php' to suit your needs.

For Web Designers

This one is for all of the web designers here... With this code, you can make your JS, CSS files and images inaccessible. And if you were paying attention to the first part of this tutorial and made custom error pages, it will take them to the 403 (access restricted) page you made. Heres the code:

RewriteCond %{HTTP_REFERER} !^http://website.com/.*$	  [NC]

RewriteCond %{HTTP_REFERER} !^http://website.com$	  [NC]

RewriteCond %{HTTP_REFERER} !^http://www.website.com/.*$	  [NC]

RewriteCond %{HTTP_REFERER} !^http://www.website.com$	  [NC] 

RewriteRule .*\.(jpg|jpeg|gif|png|bmp|css|js)$ http://www.website.com/403.php [R=301,NC,L]

Note: This code fully protects your JS and images in all browsers. But it does not protect your CSS in IE and Opera. I am still working on getting this fixed, hopefully I can fix it soon. The problem is that when CSS is accessed, it doesn't really have a http referer. It is just sort of downloaded, without really accessing the page. I know that sounds weird, but that is what it does.

Moving? No Problem!

Whenever you need to move a page, or website to a differen't domain it can be quite annoying. Mostly because of lost visitors getting 404 errors, because the page does not exist. But no more! If you are moving, or renaming a page or even a domain just use this code:

Redirect 301 /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html

And if you would like to redirect all requests to domain add this code:

Redirect 301 / http://www.newsite.com/

There are several advantages to doing your redirects this way. The most important would probably have to be the fact that when Google or Yahoo and other search engines come and crawl your site, and access the page you are redirecting, your server will send a 301 command (moved permanently), and in doing so the new page will enherent all of the pagerank of the previous page. Which you won't get just redirecting with a header_location with PHP. Not to mention the fact that depending on how that is done, search engines may read it as you spamming. Another advantage to doing this is you will save bandwith, because the redirect is done on the server side, and users won't be loading a useless redirect page.

Rewrite Those Darned Dynamic URLs!

For this example I am using the tutorials page. This is probably one of the most important elements of SEO, because search engines hate dynamic websites. Well, not really the website, but the dynamic URLs. Like in the tutorials, http://www.tutorialf...rial.php?id=133. This is not a worst case scenario, I have seen some URLs with 5 or 6 query strings. But none the less, it is best to have none at all. Use this code to fix them:

RewriteRule tutorial-(.*)\.html$ view_tutorial.php?id=$1

With this code, it will rewrite all of the tutorials on TutorialFX to a nice, SE friendly URL like so:

http://www.tutorialfx.com/tutorial-133.html

This will not only improve the rankings of the page, but accessibility as well. One thing I changed also, is I took out the '_' in the URL. I did this, because if you separate words with underscores instead of dashes, SE read them as 2 separate 'phrases'. So you will only get a keyword benefit for 'word1' and 'word2' separately if you use underscores. But if you use dashes, you will receive a keyword bonus for a search for 'word1', 'word2' and a search for 'word1 word2'. In this case it does not give much advantage, but there are other cases that it will be of great use, which is why I pointed it out.

P.S. Ghost, if you would like to rewrite the tutorials on this site with this code, feel free to do so. I don't know how you have them setup, as in if they are DB driven or not, but if you want to replace the dynamic URLS, without having to hack into your code... You can use preg_replace, to automatically change your URLs to the SEO static pages. If you need some help adapting preg_replace, I can help you with that. But I would have to see the source of the tutorials script... I don't know if you would have a problem with that or not... But let me know.

Stay tuned! More to come!
Current Version:
CS Cart 1.3.4 SP2

Current Mods installed:
Thickbox (with e-menu) : Tabbed product details : Previous & Next Navigation (with SEO mod by SWS) : Manufacturer META tags

 
  • recedo
  • Senior Member
  • Members
  • Join Date: 24-Apr 06
  • 498 posts

Posted 12 February 2007 - 09:02 AM #2

Haven't read it all through but looks good.

Many thanks
Simon

 
  • Komplex
  • Junior Member
  • Members
  • Join Date: 15-May 07
  • 17 posts

Posted 05 June 2007 - 12:59 PM #3

It would seem that I have ALOT to learn about the .htaccess structure... this is a good primer though.

Would you recommend any reading on this that can simplify this Brandito? or anyone else for that matter? ;)

I follow the structure of what your saying, but pertaining to the layout of the actual file itself, I notice that there is a number of lines already contained in there, do I add suggestions above or below these?

Is there a set structure on how the .htaccess file should be built?

As you can see, I don't have alot of experience in this realm, but am more than willing to learn...

Thanks in advance.

Komplex

 
  • Brandito
  • Member
  • Members
  • Join Date: 17-Dec 06
  • 82 posts

Posted 06 July 2007 - 07:17 PM #4

Hi, I haven't been on the forums for awhile so sorry for my late reply...

To answer your question, I would start here:

http://www.javascrip.../htaccess.shtml
http://en.wikipedia.org/wiki/Htaccess
http://httpd.apache....o/htaccess.html

Should be enough to get you started. ;)

And if you need help setting up your .htaccess, I am going to have to see your current one to help you on that.

~Brandon
Current Version:
CS Cart 1.3.4 SP2

Current Mods installed:
Thickbox (with e-menu) : Tabbed product details : Previous & Next Navigation (with SEO mod by SWS) : Manufacturer META tags

 
  • Alfie
  • Senior Member
  • Members
  • Join Date: 06-Jun 06
  • 131 posts

Posted 23 May 2009 - 11:00 PM #5

Doesnt seem that ErrorDocument 404 /404.php works with SEF on. Any ideas why?

Alfie

 
  • purplemine
  • Junior Member
  • Members
  • Join Date: 25-Aug 08
  • 13 posts

Posted 14 June 2009 - 07:57 AM #6

Alfie said:

Doesnt seem that ErrorDocument 404 /404.php works with SEF on. Any ideas why?

Alfie

Alfie, Have you created the page that you are directing to, you need to make sure that it exists?

 

Posted 06 August 2009 - 03:21 PM #7

which htaccess file do i edit??

 

Posted 06 August 2009 - 03:38 PM #8

im trying to redierct urls from my old sites urls to the new sites and its not working.... im using the httaccess in the root directory

 
  • snorocket
  • Senior Member
  • Members
  • Join Date: 15-Mar 06
  • 2,413 posts

Posted 13 August 2009 - 04:53 AM #9

Hi Mac, are the domains different from the old and new? are the urls static or dynamic? let us know as the solutions for each situation is different.
Snorocket.com is the #1 Source for Professional CS-Cart Addons.

 
  • timst
  • Member
  • Members
  • Join Date: 13-Feb 09
  • 103 posts

Posted 07 September 2009 - 04:02 AM #10

Trying to work thru this one as well. It appears that CS-Cart adds its own 404, 301, etc page when needed. I found this in the config file:

// Controller return statuses
define('CONTROLLER_STATUS_REDIRECT', 302);
define('CONTROLLER_STATUS_OK', 200);
define('CONTROLLER_STATUS_NO_PAGE', 404);
define('CONTROLLER_STATUS_DENIED', 403);
define('CONTROLLER_STATUS_DEMO', 401);

Which seems to override the .htaccess file telling it otherwise.
If I comment out the lines in config.php and in fn.control, it breaks the store from loading at all.

Seems like they really worked it into the code for the above pages and it wouild be really tough to unwind it all...
If anyone has figured this out, please post. I'd rather use my own customized 404's and 301's instead of the generic ones.

 
  • jobosales
  • Senior Member
  • Members
  • Join Date: 04-Nov 06
  • 3,114 posts

Posted 07 September 2009 - 05:52 AM #11

Have you had a go at modifying /skins/YOURSKIN/customer/exception.tpl to do your customizations?

Bob
CS-Cart 2.0.14 (testing)

 
  • timst
  • Member
  • Members
  • Join Date: 13-Feb 09
  • 103 posts

Posted 07 September 2009 - 07:30 AM #12

Tried, but everything I do cracks it or gives me a blank page.
Ultimate goal would be to force it to open up a customized 404 page instead of the generic one... or at the least, a way to turn this feature off somewhere so that the .htaccess rule would apply, but havent found a way to do that either

 
  • indy0077
  • Senior Member
  • Banned
  • Join Date: 03-Nov 09
  • 1,431 posts

Posted 14 November 2009 - 03:55 PM #13

timst said:

Tried, but everything I do cracks it or gives me a blank page.
Ultimate goal would be to force it to open up a customized 404 page instead of the generic one... or at the least, a way to turn this feature off somewhere so that the .htaccess rule would apply, but havent found a way to do that either

Hi, did you find a solution how to force the 404 error to a customizated 404 site in htaccsess?
.
CS-Cart Professional €160.00 | CS-Cart Multi-Vendor €625.00 | CS-Cart Hosting | SSL Certificates
.
CS-Cart Optimized Servers *** USA & UK VPS Servers

 
  • Raj
  • Junior Member
  • Members
  • Join Date: 24-Nov 09
  • 49 posts

Posted 19 December 2009 - 02:43 AM #14

Friends,
Currently my site pointing to non-www and I modified to .htaccess file to point www. My site is not responding. Please help.


<IfModule mod_rewrite.c>
RewriteEngine on
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} .*\/catalog\/.*
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule . index.php?sef_rewrite=1 [L,QSA]

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule ^$ http://www.domain.com/index.php [L,R=301]


</IfModule>

Kindest regards
Raj

 
  • indy0077
  • Senior Member
  • Banned
  • Join Date: 03-Nov 09
  • 1,431 posts

Posted 19 December 2009 - 02:56 AM #15

You have to change it in the config.local.php not in the htaccess.
.
CS-Cart Professional €160.00 | CS-Cart Multi-Vendor €625.00 | CS-Cart Hosting | SSL Certificates
.
CS-Cart Optimized Servers *** USA & UK VPS Servers

 
  • Raj
  • Junior Member
  • Members
  • Join Date: 24-Nov 09
  • 49 posts

Posted 19 December 2009 - 03:43 AM #16

Thanks for your quick response. I will check config.local.php right away.

Kindest regards
Raj

 
  • Raj
  • Junior Member
  • Members
  • Join Date: 24-Nov 09
  • 49 posts

Posted 19 December 2009 - 03:56 AM #17

Thank you! Thank you!....Thank you soo much....You are gem...

 
  • Raj
  • Junior Member
  • Members
  • Join Date: 24-Nov 09
  • 49 posts

Posted 19 December 2009 - 04:47 AM #18

I have modifed "config.local.php" file. When i verify with google webmaster tool I have the following message. Please advice...

"Your site is indexed as http://mydomain.com/ not as http://www.mydomain.com/. If you want to see all data for your site, you need to add and verify http://mydomain.com/ as well"

 
  • indy0077
  • Senior Member
  • Banned
  • Join Date: 03-Nov 09
  • 1,431 posts

Posted 19 December 2009 - 12:12 PM #19

Raj said:

I have modifed "config.local.php" file. When i verify with google webmaster tool I have the following message. Please advice...

"Your site is indexed as http://mydomain.com/ not as http://www.mydomain.com/. If you want to see all data for your site, you need to add and verify http://mydomain.com/ as well"

Yes,that's right. You have to tell Google that all none www pages are www now in the webmaster tools in the "Prefered domain" or something similar.
.
CS-Cart Professional €160.00 | CS-Cart Multi-Vendor €625.00 | CS-Cart Hosting | SSL Certificates
.
CS-Cart Optimized Servers *** USA & UK VPS Servers

 
  • Raj
  • Junior Member
  • Members
  • Join Date: 24-Nov 09
  • 49 posts

Posted 25 December 2009 - 03:15 AM #20

Thank you Indy.

Raj