Apache how to write url if mssing language prefix - apache

I have single page application written in Angular I am trying to achive something like this:
https://mypage.com/path/to/somewhere?key1=val1 -> https://mypage.com/de/path/to/somewhere?key1=val1
So if url is missing /en/ or /de/ prefix than it should append to the begging of path the prefix /de/.
The problem is that I am trying to solve is that we switch to multilanguage application recently and in old emails users have path without /en/ or /de/ paths.
In /www/data directory I have de and en directories.
This is my Apache configuraiton:
content_copy
<VirtualHost *:80>
ServerName localhost
DocumentRoot /www/data
<Directory "/www/data">
RewriteEngine on
RewriteBase /
RewriteRule ^../index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (..) $1/index.html [L]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de/ [R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [R]
RewriteCond %{HTTP:Accept-Language} !^de [NC]
RewriteRule ^$ /en/ [R]
</Directory>
</VirtualHost>

Related

Not redirecting from http to https automatically using .htaccess

Trying to redirect my website from http to https, but its not working... website opens on both http and https.
This is the code section presently i used in my htaccess file, but this code not auto redirecting the website with https, and making the website open with both http and https. which is confusing so much.
------------------------------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.surffares\.com$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "https\:\/\/www\.surffares\.com\/" [R=301,L]
------------------------------------------------------------------------
but this code is not working ...
This is code godaddy suggested to use in htaccess for auto redirect,
but this code only loading the front page, auto redirecting the front
page only but no other page is opening, when i click any other page,
it shows me a error not found.
---------------------------------------------------------------------
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?coolexample\.com
RewriteRule ^(.*)$ https://www.coolexample.com/$1 [R,L]
--------------------------------------------------------------------
Godaddy Customer support told you need to pull inner pages in .htaccess.
How i can auto redirect my website from http to https and also pull all the inner pages of the website.
NOTE: my website is in php mvc laravel
I've quickly built a configuration to test this scenario. In Apache VirtualHost you need to make sure that htaccess-files are respected by apache with AllowOverride.
Here is the configuration within the VirtualHost:
<VirtualHost *:80>
ServerName test.com
DocumentRoot /var/www
<Directory "/var/www>
Require all granted
AllowOverride all
</Directory
</VirtualHost>
Then, in /var/www/.htaccess, define the following:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
Hope that helps.
Jo

Stacking 301 redirects? .net to .com, www, https

I have been struggling getting this chain to work:
I want to point example.net to example.com and point everything to https://www.example.com.
So, we have: example.net, www.example.net, example.com, and www.example.com that all need to land on https://www.example.com.
I can get everything but https to work or https for the .com site only.
Any way to set this up?
First to redirect example.net to example.com , put the following code in example.net main root .htaccess file :
RewriteEngine On
RewriteRule (.*) https://www.example.com/$1 [R=301,NE,L]
Then in example.com main root .htaccess file put the following code :
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Note: clear your browser cache and test it .
I was finally able to get to this work with:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC,OR]
RewriteCond %{HTTP_HOST} ^example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.net [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#WordPress code
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect all traffic to bare domain SSL using htaccess

I'm trying to redirect all traffic through HTTPS and here's my htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteBase /
RedirectMatch 301 /index.php/(.*)$ /$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Currently here's what's going on,
If the user types in www.domain.com it redirects to: https://domain.com which is good. I would like help in configuring the file so that if the user also types in domain.com they get redirected to https://domain.com
It would be as simple as updating your pattern:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

Redirect another domain to folder without url changing htaccess

I have a htaccess redirect (without url change) to a folder where my site is. Recently i got a new domain name, but i can't get the htaccess code working on that domain.
main domain: www.test.com
folder of site: /drupal
this is the htaccess code i use (so users go to www.test.com and see the /drupal conten)
Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
RedirectMatch 301 ^/drupal/$ http://www.test.com/
Now, i have a new domain www.secondtest.com on that server, which content stands on /drupal2 but whenever i type www.secondtest.com into my browser he redirects me to www.test.com .
Is there a way to modify the htaccess like so?
www.test.com (go's invisible to folder) --> www.test.com/drupal
www.secondtest.com (go's invisible to folder) --> www.test.com/drupal2
You have to use an another RewriteCond to check the domain :
# Redirection if not www.test.com nor www.secondtest.com
RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.secondtest\.com$ [NC]
RewriteRule .* http://www.test.com/ [L,R=301]
# redirection to drupal folder if test.com
RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
# redirection to drupal2 folder if secondtest.com
RewriteCond %{HTTP_HOST} ^www\.secondtest\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal2/index.php?q=$0 [QSA]

Apache not showing index.html on address bar

I would like to show my index.html file on address bar. When you go to http://www.example.com/ to redirect it to http://www.example.com/index.html and show that index.html on address bar.
EDIT
This is my .htaccess file. I have try neo example but with no luck.
DirectoryIndex naslovnica.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* 404.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /naslovnica\.php
RewriteRule ^naslovnica\.php$ http://example.com/naslovnica.php [R=301,L]
You will have to look into Apache DocumentRoot Directory. Have a look at the following Apache Docs...
Apache DocumentRoot
You will have to create the index.html and put it in the DocumentRoot directory.
From the DOCS...
If your DocumentRoot "/usr/web" then an access to http://my.example.com/index.html refers to /usr/web/index.html.
Edited
The following document explains about hiding the index. Guess you can use it to unhhide the index as well.
Mod_Rewrite – Hide index.php
The following might be a start towards finding the right solution...
Options +FollowSymLinks
RewriteEngine on
# Redirect client requests for www.example.com/ to "www.example.com/index.html" in main doamin
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/index.html [R=301,L]