How to enable apache rewrites step by step - apache

I am writing scripts for Apache URL redirects.
I have researched the rewrite rules to be written.
Now I would like to know the procedure for implementing this.
Enabling mod_rewrites in http.conf
LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c
I have created .htaccess
Now I am not sure of the following.
1.Access rights required to do this.
2.The location to put .htaccess file
3.how to enable logs and write logs.
4.I have two web servers.Do I have to put this in both of them.
My rewrite rule looks some thing like this.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} old_domain\.com [NC]
RewriteRule ^(.*)$ http://www.new_domain.com/test$1 [L,R=301]
It would be good if some one help me with the step by step procedure to perform this.

1.Access rights required to do this.
You need write access to the folder the htaccess file is going to be uploaded to, and the htaccess file itself must have read rights by the webserver (most likely, the "apache" user)
Additionally, you need to have at the very least:
AllowOverride FileInfo
in the server config for the document root directory
2.The location to put .htaccess file
Based on the rules you have, you want it in the document root. It's denoted in the server config by the DocumentRoot directive
3.how to enable logs and write logs.
See: http://httpd.apache.org/docs/current/mod/mod_log_config.html
Specifically the TransferLog and ErrorLog directives
4.I have two web servers.Do I have to put this in both of them.
Do the same thing for each. If both webservers use the same document root, then obviously you don't need to put the htaccess file in 2 places. If they have different document roots, then put the htaccess file in both document roots.

Related

Apache subdomain vhost + subfolders of several Magento 2 installs

Basically I want to have several stores under one subdomain as sub-folders. Like this:
mysubdomain.website.com/magento2v1 - Magento 2 store #1
mysubdomain.website.com/magento2v2 - Magento 2 store #2
mysubdomain.website.com/magento2v3 - Magento 2 store #3
Steps that I did:
This subdomain will be run in a VPS server so first I needed to redirect the subdomain (mysubdomain.website.com, the one above) to another IP (the actual VPS server) using an A record.
Second I created a Vhost with the mysubdomain.website.com on that VPS server
This is the vhost on the VPS server, Apache2:
<VirtualHost *:80>
RewriteEngine On
ServerName mysubdomain.website.com
ServerAlias www.mysubdomain.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mysubdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And then in /var/www/mysubdomain I have:
/var/www/mysubdomain/magentov1
/var/www/mysubdomain/magentov2
/var/www/mysubdomain/magentov3
Now the problem is, Magento 2 needs .htaccess redirects where it redirects /pub to the actual sub-folder. I can't seem to get these .htaccess to work. The htaccess is read but it seems like the folders go a little bit crazy.
/var/www/mysubdomain/magentov1/.htaccess
/var/www/mysubdomain/magentov2/.htaccess
/var/www/mysubdomain/magentov3/.htaccess
Here's an example of this .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
Can anybody help me get this .htaccess to work? I think it's related to the folder structure, the folder base, but I don't know how to do this and didn't found any other example.
Thank you very much!
The issue most likely is that you rewrite to the absolute path /pub/ from within the configuration files in the subfolders. How should that rewritten request be processed? After that rewriting step it appears to the next round of rewriting like a request to https://sub.example.com/pub/........ None of your existing configuration files will get applied to that. And it most likely will lead to a http status 404. Which you should be able to see in your http server's error log file and also in your browser's console network tab.
Instead you should rewrite to the relative path pub/instead. Which would result in the next rewriting step getting applied to the request to https://sub.example.com/magentov2/pub/........ Which would again get (correctly) served from within that subfolder.
In general you should try to keep global rewriting rules in a common place instead of doubling them in various parallel configuration files. Even better than a common distributed configuration file (".htaccess") in the http hosts DOCUMENT_ROOT folder would be to implement such rule in the actual central configuration file. That is faster, more robust, more secure and easier to debug.

Redirect access to .htaccess to another file

I was playing around with .htaccess files and I was wondering, completely out of curiosity, is it possible to redirect access to the .htaccess file itself to another file?
Example:
User goes to http://www.example.com/.htaccess
I want the user to be redirected to index.php?foo=bar
I've tried the following:
Redirect 301 /.htaccess /index.php?foo=bar
And
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^((?s).*)$ index.php?foo=bar [QSA,L]
</IfModule>
These methods work on any other file, but the .htaccess file itself always results in a 403 Forbidden page showing up.
Is this possible? If not, then why?
If you look in your webservers configuration file you will find some directive that prevents users from accessing .htaccess or .htpasswd file from the browser. For instance if I open up my apache configuration, located at /etc/apache2/apache.conf I can spot the following lines:
#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
So you should be able to change the above to:
Order allow,deny
Allow from all
you also need to make sure that you have the correct file permission on the .htaccess file and finally restart your server using:
sudo service apache2 restart
Of course doing the above would not be very secure.

mod_rewrite URI with same name as folder or files

A simple .htaccess file with this:
RewriteEngine On
RewriteBase /
RewriteRule ^webshop$ /index.php?page=webshop [L]
... does not work for me because a have a file called webshop.php in the web root. Renaming the file solves the poblem, and changing the regex in the .htaccess file solves the problem, but still - it's only a partial match of the file name...? The only thing I can find on this is to use
DirectorySlash off
I've tried that and it made no difference.
Need some help here, there must be a pretty simple solution to this.
Most likely you have MultiViews options enabled. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Disable it by placing this line on top of your .htaccess:
Options -MultiViews

how do I force all traffic to https?

I'm trying to use .htaccess to send all traffic to https. I'm using the example in this answer: Need to redirect all traffic to https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This isn't redirecting anything for me. I placed it in var/www but it doesn't seem to have any effect. What did I do wrong?
Try [R,L] at the end of the rewrite rule:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
R redirects to the provided link, and L breaks the flow if condition is met.
For reference you can see:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
If that's the only rules you have in your htaccess file and it's in your document root then you need to check a few things because the rules are fine.
Make sure mod_rewrite is loaded. There should be a line in your httpd.conf file that looks something like:
LoadModule rewrite_module modules/mod_rewrite.so
Make sure it's uncommented.
Make sure the directory that your htaccess file is in (should be your document root) is allowed to override server settings via htaccess. In your vhost or server config, there should be something along the lines of
<Directory "/var/www/">
AllowOverride All
... (some other stuff)
</Directory>
Make sure the AllowOverride is at least FileInfo
Make sure your document root is actually where your htaccess file is in. Your vhost config should have a line like:
DocumentRoot /var/www/
Make sure the document root is for the right vhost. If you have separate vhosts for SSL and non-SSL, make sure the htaccess file is in the document root for the non-SSL vhost.

Redirect entire directory as my home directory with mod_rewrite

I've been looking for an answer to this for a while and I've tried many tricks myself, but to no avail, so I bring my questions to all you wise people.
I have a website all ready to go. The entire contents of this website is in the main "www" directory of the site. So now when I go to www.mywebsite.com/main, it goes to the www/main.php file. However, I want to move the entire contents of my website into a new folder inside the www directory, which I will call "ts". So now, my main.php file will be in www/ts/main.php
However, the problem I am having is I want all URIs to remain the same, so I want to tell the server that www.mywebsite.com/main is now found at www/ts/main.php instead of where it was before (www/), and that all subsequent content is also now found in www/ts instead of www/.
I imagine that I will need a .htaccess file in my www/ directory which tells the server that the absolute path of www.mywebsite.com is found at www/ts/, but I am not sure how to do that. I also have an .htaccess file inside my new www/ts/ directory already redirecting many URIs, but I imagine that I need to tell the server that all the files within the directory need to ignore the /ts/ URI, again, I am not sure how to do that.
If anyone is able to point me in the right direction, I would be much obliged. Thanks.
EDIT: I am on a shared server so I do not have access to any of the conf files, therefore I need a solution involving mod_rewrite instead. Thanks.
You just need to specify where is the DocumentRoot of your VirtualHost.
Check Apache configuration for your Virtualhost (it's maybe the default one, but you could create one for your named site). And check the DocumentRoot tag.
If it was something like:
DocumentRoot /foo/bar/www
Alter it to :
DocumentRoot /foo/bar/www/ts
And that's all.
Edit:
With the restriction of only accessing a .htaccess and if we assume you will limit this new folder redirection for only one DNS name (as if you make a subdir it's maybe to make some other subdirectories later) this should work:
RewriteEngine On
# limit this rule on the www.mywebsite.com DNS name requested
RewriteCond %{SERVER_NAME} ^www\.mywebsite\.com$
# limit infinite recursion, when filename is ok...
# no, in fact not # RewriteCond %{REQUEST_FILENAME} !-f
# well this one is better, no loop for mod_rewrite, even on 404
# the rule is applied only once
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# so here's the rule, map the requested file on the filesystem in ts subdir
RewriteRule ^(.*)$ /var/www/ts/$1 [L]