Apache mod_rewrite not working even trying different solutions: - apache

I have been trying to shorten URLs with apache .htaccess file, and currently I have this code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ show.php?page=$1 [NC, QSA, L]
AllowOverride All
With this code, for example https://example.com/welcome should be redirected to https://example.com/show.php?page=welcome, but it is not. When I run sudo a2enmod rewrite, I get the following:
Module rewrite already enabled
I also tried to run service apache2 restart. How can I solve it?

Related

Google load balancer with apache rewrite engine (mod_rewrite) won't work?

I recently set up a load balancer with Google Compute Engine to manage SSL certificates for me for my apache instance. (simple website)
But I am very confused. I am using a rewrite engine to change the clients URL to the proper one.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)\.?$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=302]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?example?\. [NC]
RewriteRule ^ https://%examples.co%{REQUEST_URI} [L,NE,R=302]
Rewrites: http to https, www to non-www, non-plural to plural, and all to TDL .co.
This code works without a load balancer. But with a load balancer, it's completely useless.
I've put this code in my /etc/apache2/sites-enabled/default-ssl.conf and also tried in my 000-default.conf file as well. Yes I did an apache reset after each edit. Other configurations such as Document Root all work fine in these conf files.
I also did sudo a2enmod rewrite in case you're wondering. Also, I get no errors! Other mods such as VirtualDocumentRoot which I am also using in the same conf files work perfectly.
Both http and https websites work, but their urls are just not changing according to rewrite engine.
I've also tried to use the solution here from Google forms, but with no success.
I am completely stumped on what to do at this point. Any ideas or suggestions. Am I missing headers or something? Why won't any of the RewriteEngine work?!

Using mod_write for cleanurls with Lets Encrypt

I have enabled Let's Encrypt on a server running Apache on Ubuntu 14.04 and used the auto option to re-direct all http requests to https. This is working fine.
However, I now want to use mod_rewrite to use cleanurls on my site - all I need to do is remove the .php extension from all filenames. (e.g. https://example.com/contact routes to https://example.com/contact.php)
I have tried adding the following rewrite rule to the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This configuration works fine on my localhost setup (without SSL) but does not work on the instance running Lets Encrypt.
I have tested that the .htaccess is working by adding this rule which works as expected (redirecting all www requests to the root domain)
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I suspect that there may be some conflict between the Lets Encrypt auto setup option and my mod_rewrite rule but I am stuck as to how to make them both work together.
Any help would be much appreciated.
Disable MultiViews in .htaccess:
Options -MultiViews
MultiViews (part of mod_negotiation) is likely resulting in a conflict. This does something very similar to what you are trying to achieve using mod_rewrite. With MultiViews enabled (possibly enabled in the server config, although the default is disabled), a request for /filename, will result in Apache looking for a file that matches (that would return the appropriate mime-type) by stepping through the files in that directory (essentially trying various extensions where the basename matches).
I have checked what REQUEST_FILENAME is returning - it is the path to the filename (e.g. [REQUEST_FILENAME] => /var/www/sitename/public_html/output.php)
Yeah, that's the problem. MultiViews has already "fixed" the URL (output to output.php) before mod_rewrite has been able to do its thing.

.htaccess Rule Works in Windows but not Linux

I'm migrating a system from a Windows server to a Ubuntu server. Both are using Apache2. The rule in question is as follows:
Options +FollowSymlinks
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
CheckCaseOnly On
</IfModule>
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)/.*_[pP]([0-9]\d*)\.cfm$ $1/index.cfm?productID=$2&%1 [L]
RewriteCond %{REQUEST_URI} ^/sitemap.xml [NC]
RewriteRule (.*) /includes/site_map/xml_site_map.cfm [L]
The goal is to rewrite a URL like this one: http://example.com/html/products/sample_product_201011_p5974.cfm into one like this: http://example.com/html/products/index.cfm?productID=5974 (the first rule above).
It works correctly on the Windows server and if I copy the exact .htaccess above into an online htaccess tester. Also, I can successfully use .htaccess to do domain redirects as well as the second rule above.
Is there an extra apache module I'm missing? I've got alias, rewrite, and speling enabled (among others that aren't relevant).

htaccess not working on ubuntu , tried all options

I have frusted doing all things given on web for htaccess get working on ubuntu. But I could not do it. Actually I want to run my CodeIgniter app on LAMP, its not production server. so in www directory, there could be multiple folders e.g.
www/punepchub
I access url localhost/punepchub/support/ , it gives 404 error
I want to redirect above link to localhost/punepchub/index.php/support/
using htaccess, following are the contents of htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Where as above mentioned htaccess works perfect on wamp as well as lamp given by godaddy hosting, but it is not working on LAMP server setup on my local ubuntu 12.04 desktop machine
I think apache is not accessing the htaccess file.
I tried following but It didn't worked
I have installed lamp using tasksel
Enabled Rewrite module using command: sudo a2enmod rewrite
It displays the module in the list given by command: sudo apache2ctl -M
Changed etc/apache2/sites-available/default file with AllowOverride All, Also I did same in etc/apache2/sites-enabled/000-default
I have 777 permissions for project folder and files
Also I am confused about Is there any need to create virtual hosts, as this is not production server.
Please help me I really don't know how resolve this issue now.
In advance many thanks
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /punepchub/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /punepchub/index.php [L]
</IfModule>

Internal Server Error in yii

Actually my website works in localhost, why do I get Internal Server Error in live?
I have attached my .htaccess file here, are there any errors in it?
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# Make backend accessible via url:
RewriteRule ^sysadmin& backend.php
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule ^.*$ /index.php [L]
Just try to add the following rule in your htaccess:
RewriteBase /
You should check your apache log. I had same problem, and I checked error log. There was an error that invalid command "RewriteEngine" It means rewrite module is disabled. You must enable it. For linux:
Under Apache 2+ you can simply do:
sudo a2enmod rewrite && sudo service apache2 restart
or
sudo a2enmod rewrite && sudo /etc/init.d/apache2 restart