Tip & How-To about Computers & Internet

Simple Mod_Rewrite Codefix(Virtual Directory hosts only)

A lot of hosts don't have mod_rewrite enabled, or even a basic .htaccess file. Keep in mind this is for people using a blogging system or small Content Management System, such as Wordpress or GetSimple.

This fix is clearly written to help users better understand what to do, as well as an easy copy/paste job.

  1. Simply copy and paste this code into a new Windows notepad file.
  2. "Save file as.." .htaccess - including the dot in front of htaccess.
  3. Upload the file to your public_html directory, overwriting the previous one if any.
  4. (Not a required step, use only if temp.htaccess exists.) If there is one called temp.htaccess, simply delete that one OR rename it as .htaccess and upload the new .htaccess for an overwrite.

Copy the text below this line..
AddDefaultCharset UTF-8
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9-]+)/? $index.php?id=$1

# Add a ReWriteBase line to any directory or subdirectory requiring it.
# The default directory is "/", which is the root or main directory you use.
------------------------------------End copy

Good luck to you!

Posted by on

Computers & Internet Logo

Related Topics:

Related Questions:

1 Answer

Hi, After getting ripped off by my last webhost I have decided to move my website to a different host. A friend has been helping me do this as he has a little info but we are now stuck. My site is a php site with a mysql database. We have moved the database and ftp'd the site and everything is now working on the new server except one thing - my site was designed originally to use clean urls?? When I go to my shop page (ie: mywebsite.com/shop/ I get a page not found error, yet when I type in mywebsite.com/shop.php the page displays fine. In addition, if I click on the link to go to mywebsite.com/shop/shoes/ I get a page not found error and if I manually type in mywebsite.com/shop/shoes.php I also get a page not found error even though the page shoes.php exists - funny thing is if I type in mywebsite.com/shoes.php it shows up??) I understand this is called clean urls and I need to add an .htaccess file to sort out the issue. I have looked on the ftp area of my old host and cannot find this file but have been told it is probably hidden. I have been told by my new provider that mod-rewrite is available on the server but I have no idea how to write this htaccess file. Can anyone please give me a step by step on how to do this as I am not a coder. In reponse to someone who tried to help on yahoo answers - Thank you for your answer but let me explain more clearly. I have the following files in my root directory: index.php, shop.php, checkout.php, completed.php, contact.php ... I then have a folder called templates and in this folder I have: shoes.php. clothes.php, coats.php ... The issue I am having is that most of the php files in my root directory already have href-links? pointing to places like mysite.com/shop/ and mysite.com/shop/shoes/ so when I open up the home page in my browser and hover over the links these are the addresses I see and when I click on the links I get page not found as the browser must be looking for the links in the wrong place. (these links worked fine however on the last host I used). I understand I need an htaccess file to re-direct these links to the correct places. - ie the link for mysite.com/shop/shoes/ is actually linking to a page called shoes.php located in root/templates. I understand this method was used to tidy up the urls?? ie: instead of having mysite.com/templates/shoes.php the urls shows mysite.com/shop/shoes/. I have looked at the code within some of the php pages in the root and they all have lots of links to these 'false' addresses - ie: mysite.com/shop/shoes/ when the actual file should be at mysite.com/templates/shoes.php. I don't want to go through each file one at a time as they are hundreds of links, thus the reason for trying to make this htaccess file. Thanks


Hi there,

It's been a while since I've done any Apache web server tweaking, but I hope that this suggestion might at least get you on your way.

The .htaccess file does a whole bunch of stuff, and one of them as you've found is defining the rewrite rules for mod-rewrite.
If I remember correctly (you may need help from your new provider to correctly set the permissions on your new .htaccess file if you haven't already got one), you may be able to use the following code inside the file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
RewriteRule ^([a-zA-Z0-9]+)/$ $1.php
RewriteRule ^shop/([a-zA-Z0-9]+)$ templates/$1.php
RewriteRule ^shop/([a-zA-Z0-9]+)/$ templates/$1.php

I have cobbled those rules together from memory and with some reference, so I hope that they are at least close.
In case they're not here is a brief summary of what one does as an example...

RewriteRule - Does what it says on the tin, defines a new rule.

^([a-zA-Z0-9]+)$ - This is the clever bit, called a Regular Expression. This is the bit that looks for one or more characters A to Z (upper and lower case) and numerics, after the first slash after your domain name (mywebsite.com). In the case of mywebsite.com/shop, it will find the word shop.

$1.php - This second section defines how that found word gets rewritten to make the real URL. In your case, anything like mywebsite.com/shop should get rewritten to mywebsite.com/shop.php.

The rules I've written above allow for users to type mywebsite.com/shop as well as mywebsite.com/shop/ in order to get to the real URL of mywebsite.com/shop.php

I've also provided the second pair of rules according to your description above that should hopefully redirect any requests from mywebsite.com/shop/category/ to mywebsite.com/templates/category.php

All I can suggest is that you give the above a try. Feel free to reply if it doesn't work, and I would be happy to take another look and see if I've made a silly error :)

Hope this helps.

Mar 01, 2011 | Computers & Internet

1 Answer

I now every thing about htaccess


htaccess is a simple text file that resides on a web server. It's called htaccess because the file is named ".htaccess" (with the period in front).

The purpose of the file is to allow the manager of the website to change the way a website functions by changing various configuration options for the software that runs the website (AKA, the webserver software. This software is called "Apache").

The file contains commands for the webserver software. These commands specify the new values for the configuration options.

There is too much detail about htaccess to go into here. The best way to learn about it is to read a tutorial like this one: http://httpd.apache.org/docs/1.3/howto/htaccess.html or to start by knowing what configuration change you want to make to the webserver software and then consulting the documentation in order to learn whether you can change that via htaccess and then how to do it.

Apr 05, 2009 | Apache HTTP Server

1 Answer

Blocking SumbleUpon


based on this
http://www.whois.ws/whois-ip/74.201.117.232/

NetRange: 74.201.0.0 - 74.201.255.255
CIDR: 74.201.0.0/16

so you need to block those set of ip addresses via .htaccess.
once you do this other sites hosted on those ip address can't access your site.

if you have existing .htaccess file better download that your computer first then add the lines:

order allow,deny
deny from 74.201
allow from all

if not existing:
create a file .htaccess

the content of the .htaccess file is this:

order allow,deny
deny from 74.201
allow from all

Sep 28, 2008 | StumbleUpon Social Network

Not finding what you are looking for?

300 people viewed this tip

Ask a Question

Usually answered in minutes!

Top Computers & Internet Experts

Doctor PC
Doctor PC

Level 3 Expert

7733 Answers

kakima

Level 3 Expert

102366 Answers

David Payne
David Payne

Level 3 Expert

14161 Answers

Are you a Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Loading...