I am attempting to create an .htaccess file on an Apache server to implement 302 redirects. Hosting is provided by BlueHost.
Right now I am testing an .htaccess file which consists of just a single line:
Redirect http://mydomain.com/?_escaped_fragment_=about http://mydomain.com/about.html
The file doesn't work. After learning more, I created an httpd.conf file, which looks like this:
<Directory />
Options FollowSymLinks
AllowOverride All
Allow from all
</Directory>
One of the answers to this question (Apache 302 Redirect) suggests the following may be at issue:
mod_alias is not loaded--how do I check whether it is or isn't?
Adding the httpd.conf file may require an Apache restart. Is this possible on a shared BlueHost server?
Any advice on getting the 302 redirects to work is much appreciated. Thank you!
You can't match against the query string in a redirect, you'll need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=about$
RewriteRule ^$ /about.html [L,R=302]
Related
I know there are a million posts about this, but I have read through a lot of them and made so many changes to get this to work, but I am at a loss. I have setup a redirect rule in my apache2 server web server, but it does not redirect.
My web server root folder structure:
/var/www/example/
|
|----l
|
|---index.php
|---.htaccess
/var/www/example/l/.htaccess content is:
$ cat /var/www/example/l/.htaccess
RewriteEngine On
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
I have also tried putting it in the root folder /var/www/example/.htaccess
My rewrite rule appears to be working correctly from this site:
https://htaccess.madewithlove.be?share=6cae684f-740d-4add-b711-53cdb5986681
mod_rewrite.so is installed and rewrite_module is loaded.
I ran sudo a2enmod rewrite and it was turned on
APache2.conf:
$ cat /etc/apache2/apache2.conf
<Directory "/var/www/example">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
When ever I go to https://example.com/l/i4Ikn2 I just get a 404 error.
I just want to know if I am missing something or somewhere to look. I checked the apache2 error.log and there are no error in there pertaining to this.
As #mrwhite and #anubhava mentioned in the comments below my OP, I was mixing up the way my .htaccess file was being used.
I removed the sub-directory flags (the l in the pattern) and added the QSA instead of the redirect (R=302).
This was because I was using the .htaccess file in the sub-directory.
The QSA was the correct 'option' for the redirect/rewrite (still unsure of the difference).
Original:
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
New (working):
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [QSA,L]
I know there are a million of questions about this, but I've tried the solutions in other questions and haven't got it to work in my case. I'm trying to redirect everything to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This works in my local environment, but in my staging environment it fails and I get a 404 error. I've checked that mod_rewrite is enabled with phpinfo(). I've tried changing a few things like using ^(.*)$ instead of ^ and using /index.php instead of index.php
What else could be the problem?
Figured it out. It was a problem with the configuration of apache. Whoever set it up for the site I'm working on did it like this in the site's .conf file:
<Directory "/data/path/to/directory">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# and a bunch of rewrite rules...
</Directory>
The AllowOverride None prohibits the use of .htaccess. In our local and development environments, the directory structure is different, so this code doesn't even get applied. That's why the .htaccess was working.
To solve the problem I'll either have to change the line mentioned above to AllowOverride All or add another configuration for the website I'm working on like the one above that does what I wanted to do with .htaccess.
Problems like this shouldn't come up for anyone if their environments are configured appropriately, but if anyone does run into a similar situation, check the configuration of the site in apache!
I know that question has already been asked, but for some reason, no matter how I try, redirect from https to http just doesn't work. I tried my default approach to redirect (always worked perfectly fine with http to https, thought it would go that smooth the other way as well) and a few solutions from here, but no effect. For now, this is the last method I've tried:
In Apache sites-available directory, I modified relevant .vhost entry, so the changed part now looks like this:
<Directory /var/www/SOMEWEBSITE/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
<Directory /var/www/clients/client1/web1/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
Still, connecting via https://SOMEWEBSITE gives me standard apache blank site
(It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.)
I tried inserting the rewrite rules directly in VirtualHost block and modyfing site's .htaccess file (same effect as above), also tried to create VirtualHost listening on port 443 and redirecting traffic to http, but this approach gave me an SSL error. Is there any other way or something I might be doing wrong? Or is that some ISPConfig issue I am not aware of?
You could try this solution, here on stackoverflow. It is not done within ISPConfig, but I do not think there is a way to do this from within the ISPConfig GUI anyway.
I have an .htaccess problem on my localhost Apache server. If I put anything in the .htaccess file, I get an Internal Server Error. I'm trying
Options +FollowSymlinks
or
RewriteEngine on
or
RewriteRule (.*)\.htm $1.php
But they all return the same error.
Likely your AllowOverride setting does not include neither Options nor FileInfo.
I am trying to redirect /folder to / using .htaccess but all am I getting is the Apache HTTP Server Test Page.
My root directory looks like this:
/
.htaccess
-/folder
-/folder2
-/folder3
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
What am I doing wrong? I checked my httpd.conf (I'm running Centos 5.3) and the mod_rewrite library is being loaded. As a side note, my server is not a www server, its simply a virtual machine so its hostname is centosvm.
Addition: I have found that the mod_rewrite module is loaded, but none of my .htaccess redirects seem to be working.
Addition: My httpd.conf directory directive looks like:
<Directory />
Options FollowSymLinks
#AllowOverride None
</Directory>
What does you AllowOverride say? (see: http://httpd.apache.org/docs/2.0/howto/htaccess.html)
And that would bring us to requiring this in (virtual)host/directory settings:
AllowOverride FileInfo