Apache htccess rewrite rules - apache

My site is built in Angularjs, I am trying to re direct my site to html static snapshots pages in a snapshot folder, I can mange to get google to re-direct to the snapshot folder and to the correct links inside the folder, but cant seem to get Facebook to work, any thoughts?
Here is my htaccess file below
RewriteEngine on
# RewriteRule for GOOGLE ROOT URL
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteCond %{HTTP_HOST} upfrontbeats\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /snapshots/index.html [L]
# RewriteRule for GOOGLE ROOT ALL OTHER URLS
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
# RewriteRule for FACEBOOK
RewriteCond %{HTTP_USER_AGENT} facebookexternalhit/1.1 [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_=(.*)$
RewriteRule ^(.*)$ /snapshots/$1? [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.php [NC,L]

Related

.htaccesss: allow to load resource files

I am trying to do the following.
denied directly access to example.domain.com
allow access from to example.domain.com from another.domain.com
allow to load resource files (css, js) when the the user comes form another.domain.com to example.domain.com
Points 1 and 2 i solved like this:
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC] RewriteRule ^.*$ - [F]
But i can not to load resource files. I get the error message 403.
I tried with this code, but it's not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^(index\.html|robots\.txt)
RewriteRule ^.*$ ^.*$ [L]
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC]
RewriteRule ^.*$ - [F]
I am a quite new with htaccess, can someone help me to solve this problem?
With your shown samples, please try following .htaccess rules file. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.domain\.com$ [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} ^(www\.)?another\.domain\.com$ [NC]
RewriteRule ^(.*)/?$ http://example.domain.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (?:index\.html|robots\.txt)/?$ [NC]
RewriteRule ^ - [F,L]
i have found a solution for me:
RewriteCond %{REQUEST_FILENAME} ^.+(\.js|\.css|\.json|\.jpg|\.png|\.woff2|\.woff|\.ttf)$
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC
RewriteRule ^.*$ - [F]

Apache loads relative URLS as HTTP when using HTTPS

I am trying to use SSL on my website, but the recources that have a relative URL load as http:// even when the page is using https://
This is my .htacces
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)=http [NC]
RewriteRule ^(.*)$ – [F,L]
RewriteCond %{http_host} ^tacticalghillies.nl [NC]
RewriteRule ^(.*)$ http://www.tacticalghillies.nl/$1 [L,R=301]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
What am I screwing up?
Difficult to understand all your "code".
I think you can add in the beginning :
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
Rather than mess with HTACCESS (you can easily screw up your SEF URLS is you do) just use the Joomla global configuration and force SSL to always on. Any Joomla page will automatically use SSL if you turn on the setting.

redirect with .htaccess rewrite rule to different folder on my server

I have been trying to redirect to a different folder with .htaccess when I hit a domain.
Here's my .htaccess redirect rule, can you tell me where am I wrong?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
So, what I want is, if someone visits racereadymotorsports.in, they redirect to /raceready/ directory
and on the other hand if they visit to annelies-slabbynck.com, they should redirect to /annelies/ directory
At present, this redirects twice for annelies-slabbynck.com which means that, I end up with:
http://www.annelies-slabbynck.com/annelies/annelies/ as my final url
I am running a shared hosting and do not have access to add a new configuration.
Please help.
You need to remove the [OR] flag and reproduce the same conditions with the other rule. Maybe something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Or better yet, you can use these conditions instead: RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -d
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -d
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Note that the R=301 flag in your second rule redirects the browser, unlike the first rule which only internally rewrites the request.

Redirect request and hide .php extension from the request

We have started to change our website, and we use the old and the new site together. If something not exist on the new site we just redirect them back to the old one. (This is a transparent process for the user.)
This is how .htaccess rules looks like now:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain.hu [NC]
RewriteCond %{http_host} !^domain.hu.data18.websupport.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.*)$ /static-old/$1 [QSA]
RewriteRule ^(.*)\.html$ $1.php [L,R=301,QSA]
So if we don't find the the file on the main directory, but we find it in the old one we serve the old one instead of a 404. And if the requested file has html extension then we rewrite it internally to php, because we changed this a while ago.
I want to hide the php extension (and so html will be hidden too) so if a requested uri is /something.php or /something.html then only show /something to the user. But I if I edit .htacces like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} ^(.*?)\.php[\s?] [NC]
RewriteRule ^/%1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
%{REQUEST_URI} !\.(.*)$
RewriteRule ^(.*)$ $1.php [L,R=301,QSA]
RewriteRule ^(.*)$ /static-old-from-seocms/$1 [QSA,L]
I will get an 500 internal server error, the .htaccess parsing fail.
I think this script will check if the requested file has php extension and does not exist in the main directory then redirect it to the same address without the php extension, after that the script will check if the requested uri is not exist in the main directory and does not have an extension then write it a php extension to it internally.
Try this .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain\.hu [NC]
RewriteCond %{http_host} !^domain\.hu\.data18\.websupport\.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.+)$ /static-old/$1 [L]

Redirect second domain name?

I have the following .htaccess file which works fine for say www.companyone.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
I also have the domain www.companytwo.com which points to the same site, i.e. www.companyone.com. The problem is that when someone uses www.companytwo.com which correctly goes to the original website, the url still contains www.companytwo.com, when it should become www.companyone.com.
So in effect, I am ending up with 2 duplicate sites which is bad for seo. Using the above .htaccess file, I am already ensuring that if a user types companyone.com, they automatically get redirected to www.companyone.com. I need the same thing for www.companytwo.com, i.e. companytwo.com and www.companytwo.com should become www.companyone.com.
Is this something I need to fix via the .htaccess file or some virtual server file?
You can redirect the Browser to your www.companyone.com site by using .htacces:
# Redirect to www.companyone.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^companytwo.com$ [OR]
RewriteCond %{HTTP_HOST} ^companyone.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.companytwo.com$
RewriteRule (.*)$ http://www.companyone.com/$1 [R=301,L]
# If your example works, this rewrite to index.php/$1
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]