I am trying to prevent hot linking of images and adding allowed hosts by RewriteMap in txt file dynamically, but unfortunately the condition is not working.
Here is code of VirtualHost
<VirtualHost *:80>
DocumentRoot D:\XAMPP\htdocs\test\base
ServerName base.test.dev
RewriteEngine On
RewriteMap allowedhosts "txt:D:\XAMPP\htdocs\test\base/rules.txt"
</VirtualHost>
and following is htaccess code
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^${allowedhosts:$1} [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|png)$ - [F]
and following is txt file line for host to allow to access images
http(s)?://(www\.)?base.test.dev
Please someone help me.
Thanks.
From RewriteMap
txt
A plain text file containing space-separated key-value pairs, one per line.
The problem seems to be the key part, which is the captured image type, e.g. one of gif|jpg|jpeg|bmp|png.
You might give the referrer as a key and employ a default value for unknown/invalid hosts
RewriteCond ${allowedhosts:%{HTTP_REFERER}|NOT_ALLOWED} NOT_ALLOWED
RewriteRule \.(?:gif|jpg|jpeg|bmp|png)$ - [F]
This would work as follows: the HTTP_REFERER is looked up in the map. If it is not found in the map, the default value NOT_ALLOWED is returned, and the condition will be true. In this case, a 403 Forbidden will be returned.
Related
After looking on the internet for about an hour, I didn't find the answer to my question. So I'm searching with the wrong keywords or what I want is not possible.
What I want:
I have multiple domains with different extensions, for example:
mydomain.be
mydomain.nl
Now what I want is that the mydomain.be is redirected to mydomain.nl. The solution for this I have found on the internet and shown below, with the need of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.be$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.be$
RewriteRule (.*)$ http://www.mydomain.nl/$1 [R=301,L]
With this code, when you type mydomain.be you will be redirect to mydomain.nl. But also the URL in the addressbar is changed to mydomain.nl. What I want is to keep the URL in the addressbar mydomain.be.
So, mydomain.be:
keep URL
show content of mydomain.nl
How To?
It is possible to get it done via mod_rewrite but make sure mod_proxy is enabled in your Apache's httpd.conf. Once that is done enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.be$ [NC]
RewriteRule ^ http://www.mydomain.nl%{REQUEST_URI} [L,NE,P]
Take note of flag P which is used for handling the proxy request.
Read more about flag: P in mod_rewrite
Another option without hassling with .htaccess would be to point both domains to the same document root or setting one domain as an alias for the other, depending on how you are able to configure your Apache. However, this has downsides:
If your content management system uses absolute URLs a user who clicks on mydomain.nl on a link will be directed to the mydomain.be domain (WordPress does this, as an example).
Search engines punish this behaviour by placing you further down on the search results. at least Google does, they have an interesting blog post about duplicate content. Not sure about competitors.
An example apache config could be:
<VirtualHost *:80>
ServerName mydomain.nl
ServerAlias mydomain.be
DocumentRoot /var/www/mydomain.nl/htdocs
</VirtualHost>
I am having some issues setting up my htaccess to allow multiple languages utilising the sub directory method eg:
http://www.domain.com/en/
http://www.domain.com/sw/
http://www.domain.com/ie/
Also to complicate things, the project isn't currently live, its on a dev server. For example, I am currently accessing the project at:
http://dev.domain.com/devname/projectname/
And I want the above to automatically 301 redirect to:
http://dev.domain.com/devname/projectname/en/
Here is my htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# ----------------------------------------------------------------------
# MULTI LANGUAGE SUB DIRECTORY
# ----------------------------------------------------------------------
RewriteCond %{REQUEST_URI} !^/(en|sw)/
RewriteRule ^(.*)$ en/$1 [R=301,L]
# ----------------------------------------------------------------------
# Rewrite rules
# ----------------------------------------------------------------------
## CASE STUDIES ##
RewriteRule ^casestudies/([^/\.]+).html$ index.php?controller=contents&method=viewCasestudy&link=$1 [L,QSA]
## PRODUCTS ##
RewriteRule ^products/([^/\.]+).html$ index.php?controller=contents&method=viewProduct&link=$1 [L,QSA]
RewriteRule ^([a-z{2}]+)(/)?$ index.php?controller=contents&method=viewHome&lang=$1 [L,QSA] # Default load
RewriteRule ^(/)?$ index.php?controller=contents&method=viewHome [L,QSA] # Default load
The above will actually redirect to:
http://dev.domain.com/home/webserver_dir/devname/projectname/en/
..and if I use RewriteBase it seems to just goto...
http://dev.domain.com/en/
So my question: How do I get the language URLs working correctly relative to the directory its in on my dev server, and then ideally will work when it goes live without any environment specific rules.
Bonus question: Do I need to add the ([a-z{2}]+) bit in front of all my subsequent rewrite rules or can I have a catch all that will effect all further rules?
EDIT -----------------------------
I have reduced it down to the following as suggested...
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /devname/projectname/
RewriteCond %{REQUEST_URI} !^/(en|sw)(/|$) [NC]
RewriteRule ^(.*)$ en/$1 [R=301,L]
RewriteRule ^([a-z]{2})/?$ index.php?controller=contents&method=viewHome&lang=$1 [NC,L,QSA] # Default load
... but now its redirecting to http://dev.domain.com/devname/projectname/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/, any ideas?
Have you tried the answer in the following link? It should do what you're trying to achieve.
Endless Redirect Loop by htaccess rules multi language
RewriteEngine On
RewriteBase /
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$ $1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [L]
If .htaccess must not change
Change your <VirtualHost> configuration for your DEV server project as
<VirtualHost *:80>
ServerName dev.domain.com
ServerAlias project.domain.com
DocumentRoot "/home/webserver_dir/devname/projectname"
</VirtualHost>
These changes would typically go in your httpd-vhosts.conf file. Your .htaccess files would now have
RewriteBase /
to mark root as your base directory for both your development and live servers.
If you're trying to version your projects or test multiple projects on the same dev host, then you would have to incorporate the naming scheme into the domain names instead of the URL path. For example,
<VirtualHost *:80>
ServerName dev1.domain.com
ServerAlias project1.domain.com
DocumentRoot "/home/webserver_dir/dev1/project1"
</VirtualHost>
<VirtualHost *:80>
ServerName dev2.domain.com
ServerAlias project2.domain.com
DocumentRoot "/home/webserver_dir/dev2/project2"
</VirtualHost>
The bottom line is that you can not have the same .htaccess file rules working untouched with different deployment directories unless you resort to mod-rewrite way of if-else mumbo jumbo which would just be added clutter once you've gone live.
For the rules to work transparently, Apache must only see and apply the rules on what's going live (the content that comes after /devX/projectX/ directories) which is what shifting the DocumentRoot does here for us.
If minimal mods to .htaccess are okay
Not everyone has access to Apache's .conf files. Certain hosts out-rightly reject requests to modify them. Which is why, if they have at least kept mod-rewrite enabled, a lot of website's settings can be tinkered with. One of them is to use RewriteBase to handle the different deployment directories.
So, if you keep RewriteBase / on live but change it to RewriteBase /devX/projectX/ for development, most of your RewriteRules should work as is. So, /devname/projectname/ should correctly redirect to /devname/projectname/en/.
Your use of ([a-z{2}]+) is incorrect. You probably meant ([a-z]{2}) to capture exactly two letters. If you meant to capture two or more, it would become ([a-z]{2,}). So, your default load rewrite would become
RewriteRule ^([a-z]{2})/?$ index.php?controller=contents&method=viewHome&lang=$1 [NC,L,QSA] # Default load
You're correct to assume that you would need this regex for all subsequent rules or they would fail to match. So, your RewriteRule for casestudies won't work. A simpler way to not care about the language prefix is to drop the ^ start of URL path anchor as
RewriteRule /casestudies/([^/\.]+).html$ index.php?controller=contents&method=viewCasestudy&link=$1 [NC,L,QSA]
RewriteRule /products/([^/\.]+).html$ index.php?controller=contents&method=viewProduct&link=$1 [NC,L,QSA]
Your last RewriteRule matching ^(/)?$ isn't required because you're already doing a 301 redirect for all URLs with no language directory prefix to /en/$1 above, which should ideally be
RewriteCond %{REQUEST_URI} !^/(en|sw)(/|$) [NC]
RewriteRule ^(.*)$ en/$1 [R=301,L]
Otherwise, /en would get redirected as well to /en/en.
Morning all,
I have spent Sunday attempting to rewrite a domain to no avail. I'm using MAMP and have changed my Hosts folder and MAMP httpd.conf with the following :
Addition to hosts file:
127.0.0.1 apagefor.local
Addition to httpd.conf file:
<VirtualHost *>
ServerName pagefor.local
DocumentRoot /Applications/MAMP/htdocs/pagefor
</VirtualHost>
This allowed me get rid of the pesky localhost:8888/pagefor/ to just using a nice local url of pagefor.local this all works perfectly. But now im trying to attempt to do a htaccess rewrite if a username is placed directly after the domain. Allowing me to do the following:
Change this:
pagefor.local/LEE
to this:
pagefor/LEE.local
So what have I tired? Well I thought I could do something like the following (within .htaccess):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^`pagefor.local` [NC]
RewriteRule ^pagefor.local/([A-Za-z0-9-.]+) `http://pagefor/$1.local`
Im still trying to workout how to do this, if anyone can tell me where im going... And if im on even on the right path (pun intended :) ) Thank you.
Update
I have managed to get the url to change from http://pagefor.local/lee to http://pagefor/lee.local Thanks to anubhava. The only contention I have is I originally have everything go to my index.php file which controls the entire application... So would it be possible to do the following:
RewriteCond %{HTTP_HOST} ^pagefor\.local$ [NC]
RewriteRule ^([A-Za-z0-9-.]+)$ http://pagefor/$1.local [NC,L]
Then Have the following line to get $1 into my index.php:
RewriteRule ^([A-Za-z0-9-.]+).local$ index.php?url=$1 [NC,L]
Therefore when the Url is pagefor/lee.local its on the following index.php?url=lee. I tired the above but it did not succeed. I hope im on the right track to solving this problem.
Try this instead:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^pagefor\.local$ [NC]
RewriteRule ^([A-Za-z0-9-.]+)$ http://pagefor/$1.local [L,R]
I'm trying to write a couple of rewrite rules for my drupal site in its .htaccess file and it doesn't seem to work at all. My lack of prior knowledge and experience in regex and mod_rewrite doesn't seem to help the cause either.
The URLs I'm trying to rewrite are as follows
Source:
http://www.mydrupalsite.com/somealphanumericwithspecialchars/somealphanumericwithspecialchars/somealphanumericwithspecialchars/somealphanumericwithspecialchars/Keyword1/Keyword2/UNIQUEID?queryparam1=somenumber
Destination:
http://www.mydrupalsite.com/legacystuff/UNIQUEID
My .htaccess mod_rewrite code is
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#This is the only rule written by me others are drupals
RewriteRule ^(.*)/(.*)/(.*)/(.*)/Keyword1/Keyword2/([0-9]*)?$ legacystuff/content/$5 [R=302]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
My understanding and implementation based on some quick browsing on tutorials was the first four (.*) would match with the four alphanumericspecialchars in the URL, keyword1 and keyword2 would remain constant so i could match them as such, the unique numeric id which is actually required for my destination URL (param $5 in the destination) would be captured by ([0-9*])?. I use the following vhost in my httpd.conf of my local environment
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/mydrupalsite"
ServerName mydrupalsite.com
</VirtualHost>
My trouble is that the redirect from the source to destination URL does not occur after restarting my local server and clearing the cache. Any help on where I went wrong and how to make it right would be really appreciated.
Finally got it working, added the RewriteBase for my virtual host setting and placed my RewriteRule underneath it with the trailing ? in my destination to ignore query string and the L flag. Setting RewriteLog in my httpd.conf helped understand things from a better perspective. Hopefully this helps someone else new to .htaccess in drupal.
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)/(.*)/Keyword1/Keyword2/([0-9]*)?$ legacystuff/content/$5? [R=301,L]
A lot of pages in our site uses an old subdomain for images, css and javascript files. I inherited the project from newbie developers and they didn't use any common design template. The result are hundreds of pages with static references to contents an examples are:
http://static.foo.bar/css/sample.css
http://static.foo.bar/brochure/css/sample.css
http://static.foo.bar/company/newsletter/js/common.js
http://static.foo.bar/images/version2/header.jpg
...and hundreds of other locations. I need to point them all to the main domain instead of the subdomain without creating rules for each of these on the .htaccess file. So:
http://static.foo.bar/css/sample.css
should point to:
http://www.foo.bar/css/sample.css
http://static.foo.bar/brochure/css/sample.css
should point to:
http://www.foo.bar/brochure/css/sample.css
http://static.foo.bar/company/newsletter/js/common.js
should point to:
http://www.foo.bar/company/newsletter/js/common.js
http://static.foo.bar/images/version2/header.jpg
should point to:
http://www.foo.bar/images/version2/header.jpg
I know this is possible only I'm not a server guy. Any help would be very much appreciated.
Create an .htaccess file in the document root with this content:
Add this to your server configuration, %{HTTP_HOST} check does not work from .htaccess:
<IfModule mod_rewrite.c>
# bad matching: RewriteCond %{HTTP_HOST} !^static\.foo\.bar [NC]
#
# correction, matching everything other than www.foo.bar :
RewriteCond %{HTTP_HOST} !^www\.foo\.bar$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.foo.bar/$1 [L,R]
</IfModule>
See http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
UPDATE:
Giving it a second though, if it's hosted on the same server, just add
ServerAlias static.foo.bar
to the www.foo.bar configuration to serve up the static content, too.
UPDATE #2 based on feedback:
This should work (works on my pc) in the .htaccess file, this redirects all requests coming to static.foo.bar/* to www.foo.bar/* :
RewriteCond %{HTTP_HOST} ^static\.foo\.bar [NC]
RewriteRule ^(.*)$ http://www.foo.bar/$1 [R,L]
And no, the ServerAlias command only works from a VirtualHost configuration:
http://httpd.apache.org/docs/2.2/mod/core.html#serveralias