Apache mod_rewrite not happening on localhost - apache

I can't seem to get my head around this. I just pulled down my website files from my server to work on them offline. However, my rewrite conditions are no longer working as expected.
I've been googleing for the last 3 hours and keep coming to these solutions:
put garbage in the .htaccess file to make sure it's being read. I did that and got a 500 error so it is.
Make sure mod_rewrite is enabled, and I make sure it was listed in php_info(). That's not the problem.
Other than that, I can't figure this thing out.
Here's my .htaccess. All I want to do is remove index.php from my URLs:
# Rewrite url no index.php
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule . index.php
Here's my virtual host config at the moment:
<VirtualHost *:80>
ServerAdmin xxxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I'd also like to make it known that rewrite rules seem to work a bit when they're in the virtual host config. So something like this:
<VirtualHost *:80>
ServerAdmin xxx#gmail.com
DocumentRoot "c:/wamp/www/myapp/public_html/"
<Directory "c:/wamp/www/myapp/public_html/">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
ServerName myapp.local
ErrorLog "logs/myapp.local"
CustomLog "logs/myapp.local" common
</VirtualHost>
I had other problems with this though since I have nested applications (one's at / and the other at /app2/). I also seemed to have problems with the !-f condition being ignored and it would rewrite the URLs of my images and css.
Does anyone have any ideas on how to fix this? Thanks in advance!

You should use your code like this to remove index.php from the URL.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L,QSA]
And then in your head section of your html add this to fix CSS problem.
<base href="http://myapp.local" />

Related

request matching RewriteRule returns permission denied

I am experiencing a bizarre apache error. I'd like to rewrite all requests from /api/media/fi/le/path.jpg to /media/fi/le/path.jpg
This are rewrite rules setup in virtual host:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /src/api/v1/index.php [L]
RewriteRule /api/media/(.*) /media/$1 [L]
If I point the browser to /media/fi/le/path.jpg, the image gets server successfully. If I go to /api/media/fi/le/path.jpg it complains that I do not have permission to access the resource. I must have permission, because I can access the file if no rewrites are done. How should I rewrite the last RewriteRule so that it would serve the images?
I added logging directive LogLevel alert rewrite:trace6 to see what the apache is doing under the covers. This are the results:
init rewrite engine with requested uri /api/media/32/ee/0e60731bcb220c7c7b.jpg
applying pattern '/api/v1/*' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
applying pattern '/api/media/(.*)' to uri '/api/media/32/ee/0e60731bcb220c7c7b.jpg'
rewrite '/api/media/32/ee/0e60731bcb220c7c7b.jpg' -> '/media/32/ee/0e60731bcb220c7c7b.jpg'
local path result: /media/32/ee/0e60731bcb220c7c7b.jpg
go-ahead with /media/32/ee/0e60731bcb220c7c7b.jpg [OK]
I can visit the url mydomain.com/media/32/ee/0e60731bcb220c7c7b.jpg manually and the image gets server with no problem. What am I missing?
Complete .conf file for virtual host:
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /home/user/project/src
ServerName project.com
ErrorLog logs/project-error_log
CustomLog logs/project-access_log common
DirectoryIndex index.php
<Directory /home/user/project/src>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
LogLevel alert rewrite:trace6
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /api/v1/* /api/v1/index.php
RewriteRule /api/media/(.*) /media/$1 [L]
SSLCertificateFile /etc/letsencrypt/live/project.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/project.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/project.com/chain.pem
</VirtualHost>
</IfModule>
There is weird behaviour with rewrite engine.
RewriteRule /api/media/(.*) /media/$1 [L]
Will always return forbidden. After mv media images and changing the above directive to RewriteRule /api/media/(.*) /images/$1 [L], permission error is gone.

Why htaccess redirect code not work correctly?

I have this code to redirect if REQUEST_URI not start with one of the keywords you see, but its not work in my new server!
RewriteCond "%{REQUEST_URI}" "!^/$"
RewriteCond "%{REQUEST_URI}" "!^/blog(.*)$"
RewriteCond "%{REQUEST_URI}" "!^/page(.*)$"
RewriteCond "%{REQUEST_URI}" "!^/faq(.*)$"
RewriteCond "%{REQUEST_URI}" "!^/users(.*)$"
RewriteRule "^(.*)$" "http://example.com/blog/$1" [L,R=301]
This code worked perfectly on my previous server!
by testing in https://htaccess.madewithlove.be/ it should be work!
Are you sure mod_rewrite is enabled?
You need to have an
{RewriteEngine on}
in your httpd.conf or above the conditions/rules in your .htaccess
Edit: Because of the lack of good formatting in the comments I add the enabling of the mod_rewrite logging here, too:
<VirtualHost *:80>
RewriteEngine On
LogLevel alert rewrite:trace6
ErrorLog "path/to/log"
ServerName .....
...
</VirtualHost>

laravel time out error

i have a project running in ubuntu-apache2 with laravel 5.3. The problem is that laravel works on some users but on others it doesn't. When you make the url request the page stays on hold, and you canĀ“t see anything. It's strange because i have users that are using the app normally.
I saw the apache log and i can see the 302 request but not the answer. I change in site-avaible conf the document root to the root path and a I can see the indexes, so i think the problem is in the .htacces file, although I have not made any changes. This is my .conf ->
ServerAdmin serveradmin#serveradmin.com
ServerName localhost
ServerAlias theurlofmypage
DocumentRoot /var/www/html/extranet/public
<Directory "/var/www/html/extranet/public">
AllowOverride All
Options FollowSymLinks Indexes
Order allow,deny
Allow from all
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
And this is the .htacces file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Can anyone help me??
A 302 is most likely a Laravel response, not an apache thing seeing you redirect with a 301 as far as I can tell.
I had an instance where this drove me nuts too. It happend because I had putten a redirect() somewhere in the code and some requests triggerd it. It redirected back to itself to a part with a wrong namespace which didn't fly.
All I can recommend is temporarily disable middleware and Requests (validation). If that doesn't work try to find all your abort's and redirect()'s.

redirecting domain only works for /, no subdirectories

I have just spent a good 2 hours on this problem and really can't find a solution here. I have a server hosting several websites, but for some reason one of them is behaving differently from the others: the non-www is redirecting to the www domain only for /.
I have looked in virtual hosts and .htaccess of that domain, but whatever I do, I can only get the / to correctly display. All other URLs return a 404 page (and not even the nice one I designed for the website - the server's default 404 page).
Here is my virtual hosts config:
<VirtualHost *:80>
DocumentRoot /home/frsechet/francoissechet
ServerName francoissechet.com
<Directory /home/frsechet/francoissechet>
allow from all
Options +Indexes
</Directory>
ServerAlias www.francoissechet.com
</VirtualHost>
And here is my .htaccess (but I've tried a lot of other solutions too, none of them worked, and at most it broke the rest of the website)
RewriteCond %{HTTP_HOST} ^francoissechet.com$
RewriteRule (.*) http://www.francoissechet.com/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
If any one of you has a clue where I could look next, I would greatly appreciate!
Thanks!
Edit - added the wordpress part of the .htaccess.

mod_rewrite: multiple domains on one server, mod_rewrite refuses to work on one

I have a server that serves several domains from a single IP address using Apache's Virtual Host shenanigans. Three of the sites are required to redirect to www if it's omitted from the URL.
I have the following rule in the .htaccess file of each domain:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This works for two of the three, but the third completely fails to comply. I know that the .htaccess is being hit because the framework requires all hits to be routed through index.php... and that is happening correctly. So, it's not permissions, and the .htaccess is identical (more or less) on each domain. I even looked into caching (even though that doesn't make any sense... desperation gives way to insanity!)
Please help me if you have any clue what is going on.
As requested, here's the complete vhost config, and .htaccess file...
vhost configuration:
<VirtualHost *:80>
ServerAdmin me#gmail.com
DocumentRoot /var/www/example.com
ServerName www.example.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/example.com>
Options FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
.htaccess file:
# BEGIN example.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=40]
####################################################
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
####################################################
# redirect to RoutingHandler
RewriteRule ^.*$ /index.php [NC,L]
</IfModule>
# END example.com
Bear in mind, there are two other domains set up in an identical manner... and they both work with zero issues.
I know this is an old question, but google brought me here while looking for answers on the same problem.
After searching through apache's documentation I found:
AllowOverride Directive
When this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.
So I changed "AllowOverride None" to "AllowOverride All" in the <Directory /var/www/example.com> section and it worked.