Can't get .htaccess to work on localhost - php

I am trying to get mod_rewrite to work by using a .htaccess file on my localhost server.
My apache2.conf file contains:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
And:
Include mods-enabled/*.load
/etc/apache2/mods-enabled/rewrite.load file contains:
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
The .so file exists.
service apache2 restart shows no warnings/errors
My .htaccess contains:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /process.php?path=$1 [NC,L,QSA]
And it's located at /var/www/wp/wp-content/plugins/myplugin/
process.php contains print_r($_SERVER);
I am visiting http://localhost/wp/wp-content/plugins/myplugin/randomstring and always getting a 404 error.
I tried changing the .htaccess file to a random string trying to trigger a 500 error to make sure the .htaccess was being "executed"(?), but still getting a 404 not found error.
var_dump(in_array('mod_rewrite', apache_get_modules())); returns true
what am i doing wrong? This always works on my online servers.

Check if the module is enabled using php_info(). If there is some errors.
Try the following command.
sudo a2enmod rewrite
I think if you change (/etc/apache2/sites-available/default) AllowOverride none to AllowOverride All then it will work.
If still you are getting the error, then do the steps mentioned here.
The tutorial is bit old, but you can get the module working.
I hope this helps.

Related

How to route through php script without .php extension?

I've uploaded a php script to my online server, how can I route to pages without using .php extension?
The script is running on AWS, Ubuntu 18, PHP 7.2.19, Apache 2.4.29 I've tried to use .htaccess, installed and enableed mod_rewrite, but it didn't work.
.htaccess file:
IndexIgnore * # prevent directory listing
Order deny,allow
Allow from *
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
apache2.conf file:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
when i route to file like "https://domainname.com/login", I expect to load "login.php" file, which resides in the root application folder, but instead I get 404 not found error:
Not Found
The requested URL /login was not found on this server.
and when navigate to "https://domainname.com/login.php", it works fine.

Wordpress site getting 404 if "Permalink" option set to "Post name"

I have a rather strange issue with my website. I have just decided to remove my website from one server to a new server (I no longer wanted to use wpengine.co.uk)
I have now got the site up and running however the only issue I have is if I set the "permalinks" option to "post name" as can be seen here: http://i.imgur.com/poyRQ9x.png then I get a 404.
I have the following Apache setup on a Linux box:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
My .htaccess looks like this (for now I have set the file permissions to 666):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Lastly I have enabled as can be seen from my "phpinfo()" page.
The site does work if I use the ugly looking "plain" option... but that is obviously something I don't want.
Reboot Apache after making changes to httpd.config. (Changes to pph.ini and .htaccess don't require reboots).
And, you may need to add AllowOverride All for the document root:
<Directory /var/www/html>
# ... other directives...
AllowOverride All
</Directory>
Please remove your .htaccess file from wordpress root directory, then try to change permalink setting.
Note :- backup .htaccess file before delete it.
I hope this will work for you.
Well the answer turned out to be rather strange. It appears that for this to work I needed to install some extra PHP extensions. All I needed to do was:
sudo apt-get install php-mbstring php7.0-mbstring php-gettext
Although I think that the one that finally worked was:
sudo apt-get install php-curl
I saw warnings about this but I had no idea that that would actually stop WordPress's redirection. If somebody knows why this is the case I'd be really grateful to know.

Non-functional .htaccess file & PHPinfo not executing

Very new to this stuff so please be gentle with me.
I'm running a server with Debian8, and using it to host my website, Teamspeak and a bit of file storage.
After moving my server files into /var/www/html and sorting out DNS-stuff my site is up and running - but the clean URL fix I used (with the .htaccess file) no longer functions. I wanted to find out if this was a mod_rewrite issue so I tried to check using a phpinfo.php file - which kept downloading in browser instead of executing.
My .htaccess file looks as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# Hide .html extension
## External Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,NC]
## ## Internal Redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html
AddHandler php5-script .php
Things I have done:
Installed PHP5.
apt-get -y install php5 libapache2-mod-php5
and then restarted Apache
service apache2 restart
I hoped this would fix the PHPInfo.php issue, but still no success, so I attempted to edit the .htaccess file using
AddHandler php5-script .php
I'm assuming this will work but my .htaccess file isn't functioning so I figured I need to sort that first.
I checked for Mod_rewrite using
a2enmod rewrite
and was informed the module was already enabled. Okay, not a mod_rewrite issue.
Further searching led me to find /etc/apache2/apache2.conf, where I changed
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to `
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>`
Still no dice! I also checked /etc/apache2/sites-available/ and found two files, 000-default.conf (which contains none of the above code) and default-ssl.conf (same situation). I also have /sites-enabled/ which for some reason just contains a shortcut to the previous 000-default.conf file.
I'm pretty much at my wits end now, I've Google'd this stuff to death and my poor knowledge of the workings of this stuff is clearly hindering my progress. Can anyone help?

How to check whether mod_rewrite is enable on server?

Currently I am using the hosting with lightspeed server. Hosting says mod_rewrite is enabled but I can't get my script working there. Whenever I try to access the URL, it returns 404 - not found page.
I put the same codes at another server which is running with Apache. It's working over there. So I guess, it's the .htaccess and mod_rewrite issue.
But Hosting support is still insisting with me that their mod_rewrite is on, so I would like to know how can I check whether it's actually enabled or not.
I tried to check with phpinfo(), but no luck, I can't find mod_rewrite there, is it because they are using lightspeed?
Is there any way to check? Please help me out. Thank you.
FYI: my .htaccess code is
Options -Indexes
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
I tried like this also
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
But same result.
from the command line, type
sudo a2enmod rewrite
if the rewrite mode is already enabled, it will tell you so!
To check if mod_rewrite module is enabled, create a new php file in your root folder of your WAMP server. Enter the following
phpinfo();
Access your created file from your browser.
CtrlF to open a search. Search for 'mod_rewrite'. If it is enabled you see it as 'Loaded Modules'
If not, open httpd.conf (Apache Config file) and look for the following line.
#LoadModule rewrite_module modules/mod_rewrite.so
Remove the pound ('#') sign at the start and save the this file.
Restart your apache server.
Access the same php file in your browser.
Search for 'mod_rewrite' again. You should be able to find it now.
If you are using a virtual hosts configuration file, make sure the virtual host in question has the directive AllowOverride All somewhere like this:
<VirtualHost *:80>
...
<Directory "directory/of/your/.htaccess">
AllowOverride All
</Directory>
</VirtualHost>
Basically, this states to allow processing of all .htaccess directives.
This works on CentOS:
$ sudo httpd -M |grep rewrite_module
Should output rewrite_module (shared)
console:
<VirtualHost *:80>
...
<Directory ...>
AllowOverride All
</Directory>
</VirtualHost>
sudo a2enmod rewrite
sudo service apache2 restart
If apache_get_modules() is not recognized or no info about this module in phpinfo(); try to test mod rewrite by adding those lines in your .htaccess file:
RewriteEngine On
RewriteRule ^.*$ mod_rewrite.php
And mod_rewrite.php:
<?php echo "Mod_rewrite is activated!"; ?>
PHP's perdefined apache_get_modules() function returns a list of enabled modules. To check if mod_rewrite is enabled , you can run the following script on your server :
<?php
print_r(apache_get_modules());
?>
If the above example fails, you can verify mod-rewrite using your .htaccess file.
Create an htaccess file in the document root and add the following rewriteRule :
RewriteEngine on
RewriteRule ^helloWorld/?$ /index.php [NC,L]
Now visit http://example.com/HelloWorld , You will be internally forwarded to /index.php page of your site. Otherwise, if mod-rewrite is disabled , you will get a 500 Internel server error.
Hope this helps.
If
in_array('mod_rewrite', apache_get_modules())
returns true then mod-rewrite is enabled.
you can do it on terminal, either:
apachectl -M
apache2ctl -M
taken from 2daygeek
If this code is in your .htaccess file (without the check for mod_rewrite.c)
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
and you can visit any page on your site with getting a 500 server error I think it's safe to say mod rewrite is switched on.
You can use php function
apache_get_modules
and check for mod_rewrite
<pre>
<?php
print_r(apache_get_modules());
?>
</pre>
http://in2.php.net/apache_get_modules
If you are in linux system, you can check all enable modules for apache2(in my case) in the following folder:/etc/apache2/mods-available
cd /etc/apache2/mods-available
to type: ll -a
if you want to check the available modules for php (in this case php 7 )
folder /etc/php/7.0/mods-available
cd /etc/php/7.0/mods-available
to type: ll -a
I know this question is old but if you can edit your Apache configuration file to AllowOverride All from AllowOverride None
<Directory "${SRVROOT}/htdocs">
#
# 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.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
just make a new page and add this code
<?php
if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
$res = 'Module Unavailable';
if(in_array('mod_rewrite',apache_get_modules()))
$res = 'Module Available';
?>
<html>
<head>
<title>A mod_rewrite availability check !</title></head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>
and run this page then find will able to know module is Available or not if not then you can ask to your hosting or if you want to enable it in local machine then check this youtube step by step tutorial related to enable rewrite module in wamp apache
https://youtu.be/xIspOX9FuVU?t=1m43s
Wamp server icon -> Apache -> Apache Modules and check the rewrite module option
This code worked for me:
if (strpos(shell_exec('/usr/local/apache/bin/apachectl -l'), 'mod_rewrite') !== false) echo "mod_rewrite enabled";
else echo "mod_rewrite disabled";
First you check if the module is installed
apachectl -M
It it does not shows on the list, you must activate it:
a2enmod rewrite
Now, restart your server and test
systemctl restart apache2
You just need to check whether the file is there, by typing
cat /etc/apache2/mods-available/rewrite.load
The result line may not be commented starting with #
I was having the exact problem, I solved it by clicking custom structure, then adding /index.php/%postname%/ and it works
Hope this saves someone the stress that I went through finding what the heck was wrong with it.
Simply create a php file in root and add following code
<?php
if (in_array('mod_rewrite', apache_get_modules())) {
echo "mod_rewrite is enabled";
} else {
echo "mod_rewrite is not enabled";
}

Cakephp redirection problem

I have a problem of too many redirection in one cakephp site.
Here is the site: http://pexinxas24.com/
This domain is pointed to root of the site and not to app folder.
Here is the code which is placed in htaccess of the root folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Here is the code which is places in the htaccess of the app folder.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
I am not getting any typs of PHP or cakephp errors.
This site always redirect to the root of the site. Whole code was working properly on old server. I have just changed the server and getting this error.
Looks like your are using Amazon-EC2 right? ;)
Well, probably you aren't redirected for the next reasons:
mod_rewrite it isn't enabled: Look inside of your httpd.conf, you should have the next line uncommented:
LoadModule rewrite_module modules/mod_rewrite.so
You haven't enabled override in htaccess: Look inside of your http.conf for the
directive "Directory", inside you should have something like this:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
The most important line of the previous code is AllowOverride All, on amazon instances is set to AllowOverride none by default, so you should change it to All, after that you must restart your apache server to reflect changes.
Hope it helps.
This is an old thread.
Probably if you understand about apache2 Ismael's answer is quite enough, but if you're a newbe in this subject as I am, might be useful saying that in apache2 nowadays you should "replace" the part one where he says
mod_rewrite it isn't enabled: Look inside of your httpd.conf, you should have the next line uncommented: LoadModule rewrite_module modules/mod_rewrite.so
for copying rewrite.load from mods-available to mods-enabled:
cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

Categories