Remove index.php CI and mod_rewrite not working - apache

I'm trying to remove index.php on my URL (localhost). I've followed the steps from this and because it wasn't working, I tried this. What happen? What should I do?

If you are on linux there are a few extra steps you need to take care of:
Use the a2enmod command to enable the module:
sudo a2enmod rewrite
Then restart Apache.
sudo systemctl restart apache2
mod_rewrite is now fully enabled.
But by default, Apache prohibits using an .htaccess file to apply rewrite rules, so first you need to allow changes to the file. Open the default Apache configuration file using nano or your favorite text editor.
sudo nano /etc/apache2/sites-available/000-default.conf
Then add the following new block to /etc/apache2/sites-available/000-default.conf so your configuration file looks like the following. Make sure that all blocks are properly indented.
<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
. . .
</VirtualHost>
Save and close the file. To put these changes into effect, restart Apache.
sudo systemctl restart apache2
Now, create an .htaccess file in the web root.
sudo nano /var/www/html/.htaccess
Add this line at the top of the new file to activate the rewrite engine. /var/www/html/.htaccess
RewriteEngine on
Save the file and exit.
Not it should work just fine.
If you are using xampp it is supposed to be enabled by default.
On windows

Related

apache2 not using directory specified in site-available/joomla.conf

I am running a ubuntu server.
I installed apache according to this site:
https://help.ubuntu.com/community/Joomla
I then created a file called joomla.conf in the /etc/apache2/sites-available directory. It is the same as the default configuration except I changed the DocumentRoot line to DocumentRoot /var/www/joomla. That is the directory where my joomla site is.
Some websites were saying to change the <Directory> directive but my default file didn't have one so I added it, like this:
<Directory "/var/www/joomla">
</Directory>
I ran a2dissite 000-default and a2ensite joomla.conf.
/var/www/ originally contained a html directory. Even with the above steps having been performed, apache still loads from /var/www/html/index.html. I obviously haven't edited the right file or something, help is appreciated.
Another curious thing, I don't have an /etc/httpd directory, or a httpd.conf file in my apache directory. I don't know if I need that or not?
The problem was that I had edited the files in /etc/apache2/sites-enabled which was over-riding any changes made is sites-available/. The sites-enabled directory is only meant to contain symlinks to the files in sites-available.
To fix it I first removed the non-symlink files from sites-enabled then removed and and re-added the configuration with the following commands:
$ sudo a2dissite joomla
$ sudo a2ensite joomla
(joomla.conf is the name of my config file)
Then I restarted the server with sudo service apache2 reload.
You're missing a leading slash in your directive:
<Directory "var/www/joomla">
</Directory>
Instead, it should be
<Directory "/var/www/joomla">
</Directory>
Change this, disable/enable joomla.conf and restart your apache server.
If there are problems after fixing this - the full joomla.conf listing could help.
Instead of /etc/httpd in Ubuntu you have /etc/apache2. Instead of httpd.conf you have apache2.conf as a main configuration file. This is specific for Ubuntu, feels convenient after being used to it.

Clean URLs Test Fails with Drupal 7 on EC2 Ubuntu 12.04 Instance

I've already enabled mod-rewrite with
sudo a2enmod rewrite
I've added my Drupal7 install to the apache config file located in /etc/apache2/sites-available/default (note: Is this the correct config file to edit?)
<Directory "/var/www/myDrupal">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Then I restarted Apache2 with
sudo /etc/init.d/apache2 restart
Yet it still fails the Clean URL test in Drupal. I've been banging my head against the keyboard for far too long. Anyone have any other suggestions?
If you have it residing in a "myDrupal" folder off the root, then it is in a sub directory. You need to edit .htaccess, and uncomment "RewriteBase /" on line 110.
Just change
# RewriteBase /
to
RewriteBase
Then Flush your Drupal Cache and try again.
If your .htaccess looks good, and the apache config file is set correctly with AllowOverride All, try navigating to clean-urls configuration page by yourSite.com/admin/config/search/clean-urls and enable it! Clean URL's test sometimes fails even though your server is properly configured to rewrite.
Good luck!
You need to enable mod_rewrite. You'll have to cause the following to happen during instance createion:
a2enmod rewrite
service apache2 restart
Enabling clean urls for Ubuntu 14.04
(1) Run the command a2enmod rewrite on your ubuntu (Command Line Interface) CLI to make sure rewrites are installed.
(2) vi /etc/apache2/apache2.conf
a. Under all the AllowOverride All
(3) In the .htaccess file under /var/www/ if that is your place of drupal installation make sure the Rewritebase / is uncommented and correct. If your drupal installation is **Rewritebase /var/www/drupa**l, then change it to that.
(4) Run command service apache2 restart on your ubuntu CLI and you should be good.
* Optional - make sure your root
~Good Luck

.htaccess ErrorDocument 404 not showing up

I have a server from AWS EC2 service running on Linux ubuntu and I have installed apache, php, and mysql.
I have added a .htaccess file in my document root /var/www/html.
I entered this code in it:
ErrorDocument 404 /var/www/html/404.php and it is still not showing up.
I kept entered this command multiple times: sudo service httpd restart to restart the server but no changes displayed...
How can I fix this... Did I do something wrong?
First, note that restarting httpd is not necessary for .htaccess files. .htaccess files are specifically for people who don't have root - ie, don't have access to the httpd server config file, and can't restart the server. As you're able to restart the server, you don't need .htaccess files and can use the main server config directly.
Secondly, if .htaccess files are being ignored, you need to check to see that AllowOverride is set correctly. See http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride for details. You need to also ensure that it is set in the correct scope - ie, in the right block in your configuration. Be sure you're NOT editing the one in the block, for example.
Third, if you want to ensure that a .htaccess file is in fact being read, put garbage in it.
An invalid line, such as "INVALID LINE HERE", in your .htaccess file, will result in a 500 Server Error when you point your browser at the directory containing that file. If it doesn't, then you don't have AllowOverride configured correctly.
Enable Apache mod_rewrite module
a2enmod rewrite
add the following code to /etc/apache2/sites-available/default
AllowOverride All
Restart apache
/etc/init.d/apache2 restart
If you have tried all of the above, which are all valid and good answers, and your htaccess file is not working or being read change the directive in the apache2.conf file. Under Ubuntu the path is /etc/apache2/apache2.conf
Change the <Directory> directive pointing to your public web pages, where the htaccess file resides. Change from AllowOverride None to AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
I had the same problem and found the answer and explanation on the Ubuntu Ask! forum https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working
For Ubuntu,
First, run this command :-
sudo a2enmod rewrite
Then, edit the file /etc/apache2/sites-available/000-default.conf using nano or vim using this command :-
sudo nano /etc/apache2/sites-available/000-default.conf
Then in the 000-default.conf file, add this after the line DocumentRoot /var/www/html. If your root html directory is something other, then write that :-
<Directory "/var/www/html">
AllowOverride All
</Directory>
After doing everything, restart apache using the command sudo service apache2 restart
Most probably, AllowOverride is set to None. in Directory section of apache2.conf located in /etc/apache2 folder
Try setting it to AllowOverride All
Just follow 3 steps
Enable mode_rewrite using following command
sudo a2enmod rewrite
Password will be asked. So enter your password
Update your 000-default.conf or default.conf file located at /etc/apache2/sites-available/ directory. you can not edit it directly. so use following command to open
sudo gedit /etc/apache2/sites-available/000-default.conf
Or
sudo gedit /etc/apache2/sites-available/default.conf
you will get
DocumentRoot /var/www/html
OR
DocumentRoot /var/www
line. Add following code after it.
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Make user the directory tag path is same as shown in your file.
Restart your apache server using following command
sudo service apache2 restart
In my experience, /var/www/ directory directive prevents subfolder virtualhost directives. So if you had tried all suggestions and still not working and you are using virtualhosts try this ;
1 - Be sure that you have
AllowOverride All directive in
/etc/apache2/sites-available/example.com.conf
2 - Check /var/www/ Directory directives in /etc/apache2/apache2.conf (possibly at line 164), which looks like ;
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
If there is an AllowOverride None directive change it to
AllowOverride All or just escape line
By default, Apache prohibits using an .htaccess file to apply rewrite rules, so
Step 1 — Enabling mod_rewrite (if not Enabled)
First, we need to activate mod_rewrite. It's available but not enabled with a clean Apache 2 installation.
$ sudo a2enmod rewrite
This will activate the module or alert you that the module is already enabled. To put these changes into effect, restart Apache.
$ sudo systemctl restart apache2
mod_rewrite is now fully enabled. In the next step we will set up an .htaccess file that we'll use to define rewrite rules for redirects.
Step 2 — Setting Up .htaccess
Open the default Apache configuration file using nano or your favorite text editor.
$ sudo nano /etc/apache2/sites-available/000-default.conf
Inside that file, you will find a block starting on the first line. Inside of that block, add the following new block so your configuration file looks like the following. Make sure that all blocks are properly indented.
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
. . .
</VirtualHost>
Save and close the file. To put these changes into effect, restart Apache.
$ sudo systemctl restart apache2
Done. Your .htacess should work.
This link may actually help somebody https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04
I cleared this use. By using this site click Here , follow the steps, the same steps follows upto the ubuntu version 18.04
Go to /etc/apache2/apache2.conf
You have to edit that file (you should have root permission). Change directory text as bellow:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Now you have to restart apache.
service apache2 restart
In WampServer Open WampServer Tray icon ----> Apache ---> Apache Modules --->rewrite_module
For completeness, if "AllowOverride All" doesn't fix your problem, you could debug this problem using:
Run apachectl -S and see if you have more than one namevhost. It might be that httpd is looking for .htaccess of another DocumentRoot.
Use strace -f apachectl -X and look for where it's loading (or not loading) .htaccess from.
i have a lot of sites on my virtual machine,
and i solved it only by changing config of the site in which i needed .htaccess
what i did:
sudo a2enmod rewrite
next i changed only config for particular site, not for every site "example.com"
sudo nano /etc/apache2/sites-enable/example.com.conf
inside of it i added
<Directory /var/www/example.com>
AllowOverride All
</Directory>
service apache2 restart
so it only applies for 1 site, because when i tried to apply changes to entire server it crashed, don't know why, but this solved my problem

Mod_rewrite not working on Ubuntu Server (works locally, though)

I have a local environment working fine. Pasted a test route in .htaccess and it works as expected (re-routes me to google).
RewriteEngine on
RewriteRule testpage\.html http://www.google.com [R]
I pasted the same thing on my development server (Unbutu 12.04) and it simply gives me a Not Found page. When I verify it on the devserver by running:
sudo a2enmod rewrite
It says "Module rewrite already enabled".
Edit: It also appears in the "loaded modules" section of phpinfo() and I have restarted the apache server several times since it was installed.
Any ideas?
Most likely it is because mod_rewrite is enabled but .htaccess files are disabled via
AllowOverride None
which disables checking .htaccess files (which gives You some performance gains but You have to put Your mod_rewrite code directly in apache configuration files)
Change for Your virtual host to:
AllowOverride All
For people who don't understand - http://victorpotapov.ru/blog/vkljuchaem_mod_rewrite_v_apache/2013-11-03-61
On this page you can see step by step, that you enter in your terminal on ubuntu
enable mod_rewrite
Activate the mod_rewrite module with
sudo a2enmod rewrite
and restart apache
sudo service apache2 restart
Edit virtual host to use mod rewrite within .htaccess
sudo nano /etc/apache2/sites-available/000-default.conf
Search for "DocumentRoot /var/www/html" and add the following lines
<Directory "/var/www/html">
AllowOverride All
</Directory>
Save & Exit CTRL-X, "Y" and Enter
Restart Apache
sudo service apache2 restart

Prevent access to files in a certain folder

I have a folder with a lot of .php files. I would like to deny access to them (using .htaccess). I know an option is to move this folder outside public_html, but this is not possible in this situation.
Is is possible to block access to a whole folder?
Add this to the .htaccess file:
order deny,allow
deny from all
Apache 2.4 now uses a different way to do this so the method that works for Apache 2.2 will not work. See below for the method that will work for Apache 2.4. Place this in your Apache .htaccess file or better yet in a <Directory> directive in the Apache .conf file for your site.
Using .htaccess:
If using Apache 2.2:
order deny,allow
deny from all
If using Apache 2.4 use:
Require all denied
Using Apache Configuration files:
Instead of using a .htaccess file, it is usually preferred to place the configuration directly in your Apache .conf file as follows:
<Directory "/var/www/mysite">
Require all denied
</Directory>
Or if using a virtual host:
<VirtualHost *:80>
### Other configuration here ###
<Directory "/var/www/mysite">
Require all denied
</Directory>
### Other configuration here ###
</VirtualHost>
Of course the above configurations are examples, and you will need to adjust according to your requirements.
Create .htaccess file inside the desired folder with the following contents:
Deny from all
Edit apache2.conf or httpd.conf, whatever you find in Apache2 directory (probably located in /etc/apache2). You'll need to edit/check the following:
AllowOverride ALL (in the related <Directory> tag)
AccessFileName .htaccess
Edit your site's configuration file only in case you have a <Directory> tag specified inside it and add the following line:
AllowOverride ALL
Restart your Apache2 server
service apache2 restart
The above steps are all meant for Linux environments. The same instructions would work well for Windows environments except for the paths and the server restart command.
Reference: http://www.cyberciti.biz/faq/apache-htaccess/
Just add a .htaccess file with the code Deny from all to the folder.
More info at http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html