If you have been trying to get Apache Mod Rewrite to work for the last 2 hours, then welcome.
Some keywords : mod_rewrite errors in windows or ubuntu linux. I got it working with Apache 2.2 and PHP 5.2.9.2.
Simple troubleshooting steps to make it work.
This is only a troubleshooting article for getting mod_rewrite to work. There are lots of others on the web that are useful for setting mod_rewrite up. Basically I went wrong where I had "Indexes" for my "Options". The most irritating thing in many forum posts is the incompleteness / assumption that others know how to put the entries.
There are basically 2 ways of configuring mod_rewrite.
Approach I Setting the required "Options" in the httpd.conf file
Approach II Setting the required "Options" in the .htaccess file
NOTE :
x) MAKE SURE YOU USE YOUR OWN SYSTEM PATHS FOR THE FOLDERS AND FILES !
y) Please note the parent folders .htaccess could affect your sub folder's .htacess file's setting. Hence it is recommended you try out examples given on the web as it is, including using the same folder path
z) The rewrite rules you see in this page are for 2 popular rewrite examples found on the web.
Two simple examples for Mod Rewrite / mod_rewrite
http://www.wallpaperama.com/forums/how-to-test-check-if-mod-rewrite-is-enabled-t40.html
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Approach I Setting the required "Options" in the httpd.conf file
So below changes are to be tried in the httpd.conf or our apache's equivalent httpd.conf.
1) When hitting the URL http://localhost/test-programs/modrewrite/alice.html if you get
Not Found
The requested URL /test-programs/modrewrite/alice.html was not found on this server.
then most probably your problem is you have set to AllowOverride to None.
AllowOverride None. Change this to AllowOverride All
2) Forbidden
You don't have permission to access /test-programs/modrewrite/alice.html on this server.
The above error you get if you have set the folder's Options to
Options Indexes MultiViews
Change to
Options MultiViews
i.e remove the "Indexes" and make sure you also have Options All set explicitly.
The below is sure to work if you have set it in the the right location. Use the whole configuration below EXACTLY AS IT IS, except for the folder path.
Alias / "C:/wwwroot/"
Directory "C:/wwwroot">
Options All MultiViews
AllowOverride All
Order allow,deny
Allow from all
Note MultiViews has to be added for sure.
OR the below one. Note it is required to have "FollowSymLinks" or "SymLinksIfOwnerMatch" in the options, but you still need MultiViews.
Alias / "C:/wwwroot/"
Options FollowSymLinks SymLinksIfOwnerMatch MultiViews
AllowOverride All
Order allow,deny
Allow from all
Some places to look around for errors :
Apache Error log file to check messages in :
C:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log
Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: C:/wwwroot/test-programs/modrewrite/alice.html
REFERENCE:
For your benefit, I am putting all the relevant httpd.conf entries below. This is how mine looks.
I access it on my PC as http://localhost/test-programs/modrewrite/alice.html
MAKE SURE YOU USE YOUR OWN SYSTEM PATHS FOR THE FOLDERS AND FILES !
DocumentRoot "C:/wwwroot/"
#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
#
# This should be changed to whatever you set DocumentRoot to.
#
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes
#Options Indexes All
#Options Indexes MultiViews
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
ServerRoot "C:/Program Files/Apache Software Foundation/Apache2.2"
LoadModule dir_module modules/mod_dir.so
The below is required only if you also want PHP to be working.
LoadModule php5_module "C:/SoftwareInstallations/PHP5292/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/SoftwareInstallations/PHP5292"
Here are the alias entries with relevant permissions.
Alias / "C:/wwwroot/"
Options All MultiViews
AllowOverride All
Order allow,deny
Allow from all
For more help content, search for "MultiViews" in your httpd.conf file. If you have apache2.2, maybe your main conf file for Apache maybe its apache2.conf. Best do a search for "MultiViews" in all your conf files and make sure you read the help content too.
Approach II Setting the required "Options" in the .htaccess file
If you are not allowed to modify your httpd.conf, then you can also set the "Options" properties in your corresponding .htaccess. Please note the parent folders .htaccess could affect your sub folder's .htacess file's setting.
I donot have .htaccess file in my C:/wwwroot/ or test-programs/ but only in my C:/wwwroot/test-programs/modrewrite/ folder.
That means my httpd.conf alias permissions entry is as below
Content of httpd.conf
Alias / "C:/wwwroot/"
#Options All MultiViews
AllowOverride All
Order allow,deny
Allow from all
Content of .htaccess is below
Options All MultiViews
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
RewriteRule ^alice.html$ bob.html
OR AS BELOW
Options Indexes SymLinksIfOwnerMatch MultiViews
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]
RewriteRule ^alice.html$ bob.html
For performance reasons it is recommended to have .entries in the httpd.conf itself instead of every folder.
The above 2 rewrite rules are for 2 popular rewrite examples found on the web.
No comments:
Post a Comment