Cannot enable mode rewrite - apache

I want to use mod_rewrite with Wampserver to remove "/index.php/" from addressbar. This code is suggested with Codeigniter Framework:
RewriteEngine on
#RewriteBase /
RewriteCond $1 !^(index\.php|img|table-images|robots\.txt|css|fonts|js|uploads|dbg-wizard\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
This should change this url:
mysite.local/contact_us
to this:
mysite.local/index.php/contact_us
This works with XAMPP but with Wampserver first URL generates Error 404!
I enabled mod_rewrite with Apache and there is no errors in error log.
Update:
I added .blabla in .htacess but nothing happened! It means that Apache does not read .htaccess! Why?
I added AllowOverride All to httpd-vhosts.conf.
Apache did not execute .htaccess and enabling AllowOverride in httpd.conf is not enough.
This is my httpd-vhosts.conf file:
<Directory E:\mysite\www>
Options Indexes FollowSymLinks Includes ExecCGI
#Order Deny,Allow
#Allow from all
AllowOverride All
Require all granted
</Directory>

To check that mode rewrite is working or not try these steps:
Make a folder in your root directory called htaccess_test
Create a .htaccess file, and put these code into this and save it.
options +FollowSymLinks RewriteEngine On
RewriteBase /htaccess_test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?u=$1
Create one more file called user.php and put these code into
this and save it.
<?php
if(isset($_GET['u'])){
echo $_GET['u'];
}
Now go to browser type http://localhost/htaccess_test/put_some_value_here.
If that is working than the problem is not in your apache setting.
Make sure you are editing the right .htaccess file. The .htaccess file you need to edit is in your projects' root folder, not in application or any other folder. And put these code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /root_directory_of_your_project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Related

Vue-router history mode isn't working in production

I'm new to vue and I'm having an issue with vue router. I'm using Vue CLI 3.
History mode is working perfectly on a local server but in production mode it shows a 404 when refreshing
I know this is a common issue, so I followed the documentation of Vue Router. So I put this piece of code in a .htaccess file at the root of the project, but it doesn't seem to work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Since I'm a beginner I must miss something, any idea?
Thank you for your help !
On Google Cloud Compute, running Debian and serving via Apache.
This was painful as mod_rewrite would just not work. Turns out the issue seems to be with Apache in prod (especially if you do it yourself), where the defaults nail you.
Case in point the following rule in /etc/apache2/apache2.conf :
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Thus, the files under /var/www/http, which means any .htaccess rewrite rules will not be allowed. Luckily once we know this, we can enable the rewrite:
<Directory /var/www/html>
AllowOverride All
</Directory>
The .htaccess rewrite rules (via mod_rewrite) will now be applied as expected:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

How can I hide file extensions in the URL of my site with Apache?

Before anyone says this is a duplicate - I tried all other similar questions without any successful results.
I use apache.
I am trying to hide file extensions in the URL. For example, index.php would show as index in the URL.
So what I did:
1) I created a .htaccess file, pasted this into it:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [NC]
RewriteCond %{REQUEST_URI} ^/(.+?)/$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html [L]
RewriteCond %{REQUEST_URI} ^/(.+?)/$
RewriteCond %{DOCUMENT_ROOT}/%1\.php -f
RewriteRule ^ /%1.php [L]
2) Uploaded .htacccess to www/html folder
3) Restarted apache
4) Ran sudo a2enmod rewrite
5) Restarted apache again
6) Went to /etc/apache2/sites-enabled/000-default.conf and pasted this in the <VirtualHost *:80> tab:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
7) restarted apache
And... file extensions still show on my website. This is my first time playing around with .htaccess so can someone please explain what I am doing wrong and the steps I should take to do this correctly?
Best thing to do is break down the rules and apply them one at a time.
Remove everything in .htaccess and addthe below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now see if files with .php extension resolve correctly.
Then add the .html rule. It is exactly the same just change the extension.

xampp apache rewrite not working

I have a folder called crm in htdocs which contains a fresh laravel 5.1
project and i am trying to acess it via http://localhost/crm/
but it just brings the index of page containing the directory contents
instead of the page mapped in my routes.php as
Route::get('/', function () {
return view('panel');
});
i have checked that apache mod_rewrite is enable in httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
then the .htaccess file in crm/public folder contains
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I have also tried to change it to
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
without success.
You need to point Laravel to public directory to make it work. For example, if you've installed Laravel in C:/xampp/htdocs/ directory, you need to use these settings:
DocumentRoot "C:/xampp/htdocs/public"
<Directory "C:/xampp/htdocs/public">
Do not edit .htacces inside public folder. Try to change settings and load Laravel website by going to localhost first.
When you've edited Apache config file, you should restart web server.

Rewrites to Mac OSX WebApp not working

I've created a WebApp on Mac OS X 10.9. If I just go to something like the index.html it works fine, so I know the apache config there is working. Now I'm trying to get a rewrite rule to work for Restler and am having issues. My httpd.conf file looks like this:
Alias "/dts" "/Library/Server/Web/Data/WebApps/dts"
<Directory "/Library/Server/Web/Data/WebApps/dts">
AllowOverride All
Options Indexes -MultiViews FollowSymLinks
</Directory>
Now I want a path like https://myserver.com/dts/api/foo to redirect to the index.php file in the api directory, so I put a .htaccess file in the api subdirectory:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Library/Server/Web/Data/WebApps/dts/api
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<IfModule mod_php5.c>
php_flag display_errors On
</IfModule>
The last line of my rewrite log has the correct path showing:
(1) pass through /Library/Server/Web/Data/WebApps/dts/api/index.php
But what the browser returns is:
The requested URL /Library/Server/Web/Data/WebApps/dts/api/index.php was not found on this server.
If I do an ls -l of that path it's absolutely there. What am I doing wrong?

.htaccess working locally, but not on 1and1 server

I uploaded the current .htaccess file to a 1and1 server (actually 1und1.de, but I guess it's the same) and I'm geting a 500 Internal Server Error.
Options -MultiViews
RewriteEngine On
RewriteBase /lammkontor
RewriteRule ^categories/([^/\.]+)/?$ index.php?url=category.php&cat_url=$1 [L]
RewriteRule ^categories/([^/\.]+)/([^/\.]+)/?$ index.php?url=product.php&cat_url=$1&prod_url=$2 [L]
RewriteRule ^categories/([^/\.]+)/([^/\.]+)/recipes?$ index.php?url=recipes.php&cat_url=$1&prod_url=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+\.php)/?$ index.php?url=$1 [QSA]
This .htaccess works perfectly on my local MAMP server.
When I test the CGI-Monitor in the control-center with an example file I get
- cgi: File not present or has invalid modes
(no output)
The only file working now is index.php
Thanks for your help!
Actually I solved my problem adding a slash to the beginning of every Rewrite Rule, like:
RewriteRule ^(.+\.php)/?$ /index.php?url=$1 [QSA]
instead of
RewriteRule ^(.+\.php)/?$ index.php?url=$1 [QSA]
Thanks!
By default apache's htaccess rights are off or limited depending on your host.
I suspect its your Options -MultiViews causing the error.
Check your httpd.conf and check that MultiViews is allowed like below.
<Directory />
Options FollowSymLinks
AllowOverride Indexes Options=All,MultiViews
Order deny,allow
Deny from all
</Directory>
Just put a slash before the url. eg /index.php instead of index.php at the end of the rule