Im trying to move a bunch of rewrite rules from a .htaccess file to a the apache config files, I get no errors and placed this within the VirtualHost section of the sites config file:
<Directory /var/www/da/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^botswana/central-kalahari/$ /central-kalahari/ [R=301,L]
</Directory>
Rewrite Rules placed in server config / virtual host context will start with leading slash -- that is one of the differences from .htaccess behaviour. Therefore use this one:
RewriteRule ^/botswana/central-kalahari/$ /central-kalahari/ [R=301,L]
Related
I have a very simple aim: Rewrite the URLs of my PHP app such that localhost/slim_demo/archive is interpreted as localhost/slim_demo/index.php/archive by the system but the user sees the former. EDIT: The system is behaving as if no rewriting is taking place. The latter version of the URL returns data, but the former throws a Not Found error.
I used the following .htaccess file, but it's not happening (by the way, as the second line says, uncommenting it rejects all requests, which shows that .htaccess is alive and kicking):
Options +FollowSymLinks -MultiViews -Indexes
#deny from 127.0.0.1 #Uncomment to prove that .htacess is working
RewriteEngine On
RewriteRule ^slim_demo/(.*)$ slim_demo/index.php/$1 [NC,L]
And here's the relevant section from my apache2.conf:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /media/common/htdocs>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
I also did a2enmod rewrite and service apache2 restart. Frustrated, I also added this to my sites-available and did a restart:
<Directory /media/common/htdocs/>
Options +FollowSymLinks -Indexes
AllowOverride All
</Directory>
Not sure what else I need to do!
So if this .htaccess file is in the slim_demo directory, your RewriteRule never matches :
In Directory and htaccess context, the Pattern will initially be
matched against the filesystem path, after removing the prefix that
led the server to the current RewriteRule
(The pattern is in your case the ^slim_demo/(.*)$ part).
This means that when you try to get the URL localhost/slim_demo/archive the slim_demo part is removed, and your rule never can match.
So you need:
RewriteRule ^(.*)$ index.php/$1
but this will bring you in an infinite loop and a 500 error. You must trigger this rule only if the REQUEST_URI does not have the index.php.
All together becomes:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(?!/slim_demo/index\.php).*$
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]
I'm using MAMP with alias named works.
Codeigniter is at localhost/works/project/web
controllers doesn't work without index.php before them (localhost/works/project/web/index.php/auth/register)
$config['base_url'] = 'localhost/works/project/web/'; //with http://
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
(I tried all of them for uri_protocol)
I created and edit .htaccess file at /User/me/works/project/web/
I tired all the .htaccess files about this issue.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Users/me/works/project/web
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
creates error, which is:
[error] [client ::1] File does not exist: /Users/me/works/project/web/auth
it is same when I use RewriteBase /
mod_rewrite is active in phpInfo.
I couldn't find the solution.
Your RewriteBase is wrong. It should not be the relative file path for your server, it should be the relative URL path for your rewrite rules and such. More info here.
If you've ever used HTML's <base> tag, it's pretty much the same principle. If your main localhost URL is http://localhost, and your project is in the subfolder http://localhost/my-project, then you would use RewriteBase /my-project/.
If you didn't use RewriteBase, and your project is in a subfolder, you'd have to add the subfolder to every one of your URLs and rewrites, like this:
RewriteRule ^(.*)$ /my-project/index.php/$1 [L]
Food for thought:
Start your .htaccess files as simple as possible. The more you throw at it right away, the more that can go wrong, and the harder it is to debug. Especially if you're a noob to .htaccess files. Don't blindly copy and paste -- figure out what things actually do.
The system folder access part is redundant now -- the system folder has its own .htaccess to restrict requests, just like the application folder.
1.Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.use this in .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
and enable rewrite mode using below command
a2enmod rewrite
and Edit the file /etc/apache2/sites-enabled/000-default
change the AllowOverride None to AllowOverride All.
and finally Restart your server
You certainly don't have mod_rewrite on or you did not install it with MAMP
Open your httpd config file and change
# 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
To
AllowOverride All
uncomment this line:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Also make some changes to users here: /etc/apache2/users/username.conf
LoadModule php5_module libexec/apache2/libphp5.so
DocumentRoot "/Users/username/where/ever/you/like"
<Directory "/Users/username/where/ever/you/like">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Get more info here http://adam.merrifield.ca/2010/08/09/apache-mod_rewrite-php-and-os-x-10-6-snow-leopard/
Restart MAMP
To find out if mod_rewrite is on, follow this answer in Stackoverflow:
How to check if mod_rewrite is enabled in php?
Just to be sure your .htaccess is not the issue, could you try this one. It's been tested:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Users/me/works/project/web/
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads|css|images|js)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Access to certain folders are granted above and the rewritebase has a trailing slash.
Make sure you have any other nested .htaccess overriding this very one.
I fixed the problem by instead of using .htacces, I write them to httpd.conf.
I checked the AllowOverride is All but I'm not sure why .htaccess isn't working. Because of I lost too much time with this issue, I'm going to use this way.
Thanks for the answers.
Use AllowOverride All on your vhost:
<Directory "/path/to/your/site">
....
AllowOverride All
</Directory>
Open your httpd config file
LoadModule rewrite_module modules/mod_rewrite.so then AllowOverride All
<Directory "C:/xampp/htdocs"> //example for xampp
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have installed codeigniter in a url like the format: http://11.12.34.45/~project/
I tried different suggestions and finally this following url provided the solution:
https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html
You need to change two files of Apache,
httpd.conf
httpd-vhosts.conf
change this in httpd.conf
<Files ".ht*">
Require all denied
</Files>
to,
<Files ".ht*">
Require all granted
</Files>
and add below code in httpd-vhosts.conf,
<VirtualHost *:8000>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
My problem is that my .htaccess file on my local server is not being read. The settings in the VirtualHost file seem to always take precedence.
I have tried the following:
Enabled mod_rewrite
Changed the AllowOverride to All but this causes a HTTP Error 500 Internal server error. I have tried it with various options but it always causes a 500 error.
I am using a VirtualHost file on Ubuntu which looks like the following:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /web/website
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /web/website>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
In my .htaccess file under /web/website I have the following rules (which are not being read):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit
RewriteRule ^(.*)$ ogtags.php?$1 [L,QSA]
ErrorDocument 404 /404
ErrorDocument 401 /401
One thing I tried which did work was appending these rules directly into the VirtualHost file, but I would like my .htaccess file to work! Is that such a big ask? :(
Edit: So I looked in my apache error.log and it says Invalid command 'Action', perhaps misspelled or defined by a module not included in the server configuration referring to my .htaccess file. There doesn't seem to be a module called Action which I can enable. Any ideas?
Edit 2: I noticed that my httpd.conf file is blank. Should this matter since I am using VirtualHost files?
After looking at my apache error.log I realised I just had to enable the Apache actions module:
sudo a2enmod actions
And no more 500 Internal Server Errors!
Hope this helps somebody down the line :)
You miss the RewriteBase / directive
Add it after the RewriteEngine directive :
RewriteEngine On
RewriteBase /
I am trying to get the .htaccess working in MAMP. The content of the .htaccess is a simple redirect line, but the entire .htaccess file seems to have no effect, even when I change it to contain invalid data.
Is there any settings within MAMP I need to change to enable .htaccess files?
In httpd.conf on /Applications/MAMP/conf/apache, find:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
Replace None with All.
Restart MAMP servers.
Go to httpd.conf on /Applications/MAMP/conf/apache and see if the LoadModule rewrite_module modules/mod_rewrite.so line is un-commented (without the # at the beginning)
and change these
from ...
<VirtualHost *:80>
ServerName ...
DocumentRoot /....
</VirtualHost>
To this:
<VirtualHost *:80>
ServerAdmin ...
ServerName ...
DocumentRoot ...
<Directory ...>
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory ...>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I'm using MAMP (downloaded today) and had this problem also. The issue is with this version of the MAMP stack's default httpd.conf directive around line 370. Look at httpd.conf down at around line 370 and you will find:
<Directory "/Applications/MAMP/bin/mamp">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
You need to change: AllowOverride None
To: AllowOverride All
If you have MAMP PRO you can set up a host like mysite.local, then add some options from the 'Advanced' panel in the main window. Just switch on the options 'Indexes' and 'MultiViews'. 'Includes' and 'FollowSymLinks' should already be checked.
The problem I was having with the rewrite is that some .htaccess files for Codeigniter, etc come with
RewriteBase /
Which doesn't seem to work in MAMP...at least for me.
I have MAMP v 6.6.2 (year 2022), and I was trying to make working php friendly URLs on my localhost Apache, by adding '.htaccess' file to my website root directory ("localhost/mywebsite/.htaccess"):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
Which didn't work. But I tested it on cPanel hosting on a real domain and it worked fine.
So I have tried to change httpd.conf with these advises:
https://stackoverflow.com/a/21411933/3936149 - didn't help
https://stackoverflow.com/a/7670598/3936149 - didn't help
https://stackoverflow.com/a/19204983/3936149 - didn't help
In the end I came up to an advise:
https://stackoverflow.com/a/16106140/3936149 - and it worked!
I simply commented out the second line of the code in my '.htaccess' (located at "localhost/mywebsite/.htaccess") file and that's it:
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
After it started to work I reverted all the changes in httpd.conf file, I have done above, and it was still working.
I uploaded the .htaccess to the server and received an Error 500 (Internal Server Error).
And in the error log I had the following error:
.../.htaccess: RewriteEngine not allowed here
But mod_rewrite.so is enabled.
So, do I need to change
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
to
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
in the /etc/httpd/conf/httpd.conf file?
Or could it be something else? The .htaccess file should be okay, because it works perfectly fine on my localhost. I just don't want to screw anything up.
Here's part of my .htaccess file:
Options All -Indexes
Options +FollowSymLinks
RewriteEngine On
minimum configuration for your .htaccess to work:
AllowOverride FileInfo Options
allowing all configuration will work as well:
AllowOverride All
Let's say your DOCUMENT_ROOT is /home/foo/web then have this config in your httpd.conf file:
<Directory "/home/foo/web">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
This should take care of RewriteEngine is not allowed error you're getting.
Just an idea, this happened to me, and I've lost a lot of time trying to solve it.
If you have a directory like d:/web/my-sites/some-site/
And you place the .htaccess on d:/web/my-sites/some-site/.htaccess (where it supposed to be).
If you have ANY .htaccess files before this directory, Apache reads that files, and blocks the execution of the entire path, producing an internal server error.
I.E.: You have d:/web/my-sites/.htaccess
In httpd version 2.4 (2.4.3 and 2.4.4), take a look at /etc/httpd/conf.d/wordpress.conf
There is one entry for:
....
Change:
"AllowOverride Options"
to
"AllowOverride All"
in wordpress.conf also in addition to changing httpd.conf. Restart the http server before the changes will take effect.
Also, just make sure you are editing the correct config file. I had created a file under /etc/apache2/users/USERNAME.conf but was editing /etc/apache2/httpd.conf.
Removing the USERNAME.conf file worked.
Try to change username.conf file on Mac under /etc/apache2/users/username.conf to
<Directory "/Users/akyoo/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
My .htaccess file looks like this
#Allow from all
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This works fine for me.
Solution for Ubuntu users
I was getting this type of error from the Google Cloud instance after checking the logs from the /var/log/apache2/error.log
.htaccess: RewriteEngine not allowed here
To get rid of the above error & 500 Internal Server Error, follow these simple steps
sudo nano /etc/apache2/apache2.conf
Then add these snippets of lines
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
After you’ve made that change, make sure to restart the server:
sudo service apache2 restart
This worked for me in getting rid of 500 Internal Server Error hosted on Google Cloud instance
you could use something like this in your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|html|test)
RewriteRule ^(.*)$ /index.php/$1 [L]
this code simply means, that anything not pointing to index.php or html or test should be directed to index.php!
Hope this is helpful!