Redirecting PDF links from another domain using htaccess - apache

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]

Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).

Related

force SSL for single .html page without php or anything else just using .htaccess

After several hours of trying a myriad of suggestions for .htaccess I have given up and decided to ask here.
I have a single html page that needs to be served via SSL. It is a single file with the .htm extension and it contains no php whatsoever. If anybody accesses this page via typing it in or clicking on a link from a non SSL page, I want that person to be redirected to or shown the SSL version of that page. Only https://example.com/myfile.htm should be allowed. The rest of the site can go without SSL, just this one page needs it.
Please help.
Try this in your .htaccess file.
RewriteEngine On
RewriteCond %{HTTPS} !^on$
RewriteRule ^myfile\.htm$ https://www.example.com/myfile.htm [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^myfile\.htm$ https://example.com/myfile.htm [L,R]
SSL for HTML only page / SSL for JavaScript only page:
If anyone needs to set SSL (HTTPS) for page that uses html only (without PHP, nodeJS etc.) just put .htaccess file in the same folder as index.htm page.
Content of the .htaccess have to be:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I had trouble finding how to, so I think that this thread is the best to hold this answer.

Apache Rewrite: favicon in %{HTTP_HOST} directory

My software supports multiple domains pointed at the exact same directory, I use shared hosting. I need to have each domain's favicon load from directories with their respective host names. Here is a visual...
http://www.example1.com/favicon.ico
public_html/www.example1.com/favicon.ico
\
http://www.example2.com/favicon.ico
public_html/www.example2.com/favicon.ico
\
http://www.example3.com/favicon.ico
public_html/www.example3.com/favicon.ico
I've tried some rewrites along the lines like this without any success...
RewriteEngine on
RewriteRule ^favicon\.ico$ %{HTTP_HOST}/favicon\.ico
Things to keep in mind...
1.) I use shared hosting so remember that the answer I need should be short and simple.
2.) I will only accept a DYNAMIC answer, I will only use the %{HTTP_HOST} variable and NOT a static domain name as I will not be manually editing my .htaccess file every single time I add a domain name.
3.) I may end up putting a .htaccess file in those sub-directories though I do not at the moment, an exception for the favicon would be greatly appreciated though is not necessary for me to accept the answer.
4.) I'll be more than happy to make any clarifications.
Use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(favicon\.ico)$ %{HTTP_HOST}/$1 [L,NC]
I have been struggling with this issue too but I finally fixed it using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/favicon\.ico$ /sites/all/themes/mytheme/favicon.ico
</IfModule>
I stuffed this into a virtual host declaration. You can do this for each of your virtual hosts, all you need to do is point the second part to the correct icon!
This solves all of my favicon problems, even for Firefox :)
(Tested on FF25, Safari 6.1, IE8 and IE10)
This one worked better in my case
RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L,NC]
To support all possible browsers and platforms, in addition to favicon.ico file, need to have files such as android-chrome-192x192.png, apple-touch-icon.png, favicon-32x32.png, etc...
Here is rewrite rule to support them all:
RewriteRule ^(favicon.*\.(ico|png)|apple-touch-icon.*\.png|android-chrome.*\.png|mstile.*\.png|safari-pinned-tab.*\.svg)$ /favicons/%{HTTP_HOST}/$1 [L,NC]
This will serve favicons including Apple Touch, Android Chrome, Windows and other favicons from /favicons/<DOMAIN_NAME> folder.
I had a problem with favicon in files on subdomain
I was struggling with redirect for favicon in htaccess for only one subdomain for a long time.
My case was that all domain take favicon from public/ directory. One subdomain (let's call it 'subdomain_a') is configured to take it from another directory and it works.
Problem appeared when a file was opened on subdomain_a. The favicon in file view (f.e. pdf-viewer) was taken from public/ directory, not from configuration of subdomain_a.
Here is my solution:
# Redirect for favicon
RewriteCond %{HTTP_HOST} ^www.subdomain_a.com
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteRule ^(.*)$ /path/to/favicon/for/new/domain/$1 [R=301,L]

htaccess folder rewrite

I've been reading multiple posts on here about htaccess folder rewriting but none seem to fit my question (properly).
My question is:
I have 2 sub folders on the server, website1 and website2.
When a user goes to www.foo.com I wish the visual url to remain the same but want the server URI to go to /website1/ where it will load the index.php for website1
I then want the same thing only when a user goes to www.bar.com again the url does not change but this time it links to /website2/ where it will load the index.php for the 2nd website.
Would really appreciate some help with this as I'm still learning about rewrites. Examples with explanations would be highly appreciated. Also any advice of best practice (if their is any) would also be appreciated.
KingCrunch is right -- the proper way to setup such environment is to use <VirtualHost> directive in Apache config file.
If, for whatever reason this needs to be dona via rewrite and .htaccess .. then you need mod_rewrite to be enabled and .htaccess files to be allowed to contain rewrite rule (AllowOverride directive).
Here are the rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# rule #1
RewriteCond %{HTTP_HOST} =www.foo.com
RewriteCond %{REQUEST_URI} !^/website1/
RewriteRule (.*) /website1/$1 [L]
# rule #2
RewriteCond %{HTTP_HOST} =www.bar.com
RewriteCond %{REQUEST_URI} !^/website2/
RewriteRule (.*) /website2/$1 [L]
This code is to be placed in .htaccess file in root folder. If placed elsewhere (e.g. configuration or virtual host context) some tweaking may be required.
Fist rule is for www.foo.com and second for another domain name. These rules are pretty much the same. We tell Apache to check domain name (via {HTTP_HOST} request variable), and if it matches our domain rewrite (internal redirect) URL into one folder deeper. The second condition is to prevent a rewrite loop (to not to rewrite already rewritten URL). It is necessary as Apache, after executing rewrite, goes to the next rewrite iteration (that is how it works), and this condition is required to stop the loop.
Useful link: http://httpd.apache.org/docs/current/rewrite/
I believe that you need to use only RewriteCond and RewriteRule directives. Take a look 'Virtual User Hosts' at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html.
The logical is the same. (I think.)

.htaccess: Redirect depending on accessed url

I want to, in my .htaccess, redirect the user to another url depending on what the user accesses.
In this case, http://example.com/awesome.com and http://awesome.com is the same site, and if the user is accessing http://example.com/awesome.com, I want him or her to be redirected to http://awesome.com.
Is this feasible?
Edit: With the help of answers, I came up with this working solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^awesome.com$
RewriteRule ^(.*)$ http://awesome.com/$1 [R=301]
you can use mod_rewrite (apache2 module)
this is the .htaccess that i use in order to redirect from my old domain to my new one (while keeping the link strcture e.g www.domain1.com/link/linkb.html becomes www.domain1.gr/link/linkb.html)
RewriteEngine On
RewriteCond %(www\.)?domain1\.com$ [NC]
RewriteRule .* http://www.domain1.gr%{REQUEST_URI} [R=301,L]
google mod_rewrite for more information (syntax etc)
Not entirely sure about .htaccess, but you could just use server code on your 404 page to redirect them appropriately; this way you could collect stats, setup a toolbar, or whatever other actions you might want to take.
.htaccess is about authorization, not redirection. I recommend you look at the redirection support for Apache (or whatever web server you're using), which is a much better fit for this problem and just make sure your .htaccess/authorization is in line with the target.
This rule should do it:
RewriteRule ^awesome\.example(/.*)?$ http://www.awesome.example$1 [R=301,L]
Check the Redirect & RedirectMatch options in apache. For simple cases, like yours it's simplier than a mod_rewrite.
Redirect /awesome.com http://ww.awesome.com
or
Redirect permanent /awesome.com http://ww.awesome.com
Now, if example.com and awesome.com are on the same apache server and same virtualhost you're maybe mising the named bases virtualhost things and you're maybe trying to make something really more complex than a simple named base virtualhost definition.

.htaccess require SSL for a particular URL

I want to force Apache to use HTTPS for a particular URL in the following form:
https://www.example.com/signup/*
so
if someone goes to any of the following example URLs directly, Apache will forward the URL over to the HTTPS equivalent site.
e.g.
http://www.example.com/signup --> https://www.example.com/signup
http://www.example.com/signup/basic+plan --> https://www.example.com/signup/basic+plan
http://www.example.com/signup/premium --> https://www.example.com/signup/premium
Anyone know how?
Thanks in advance
Thank Murat,
Yours almost worked but figured out how to get it to exactly work.
The following is what works:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Notice that I didn't include somefolder in the www.domain.com rewriterule
I think this was what i used:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/somefolder/?
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
(from here)
You can use the Redirect directive:
Redirect 301 /signup https://www.example.com/signup
This will automatically preserve anything following /signup in the URL. Be sure to configure this directive only on your non-SSL site, or it might get into a recursive loop!
You should take a look at mod_rewrite documentation
I used the following to require the checkout section of a website to require SSL:
<Directory "/var/www/html">
RewriteEngine on
Options +FollowSymLinks
Order allow,deny
Allow from all
RewriteCond %{SERVER_PORT} !^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule ^checkout(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>
So for example, hitting http://www.example.com/checkout redirects to https://www.example.com/checkout
The rule will skip file extensions that are typically included within a page so that you don't get mixed content warnings. You should add to this list as necessary.
If you want multiple pages change the RewriteRule to something like:
RewriteRule ^(checkout|login)(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Of course, the directory should match the actual path on your server. This page may also help with some more information for your specific needs: http://www.whoopis.com/howtos/apache-rewrite.html
I'm using this on a website that runs Plesk 8.6 but that shouldn't matter. This is in my vhost.conf file which is like putting it in your httpd.conf file. I'm not sure if you'd need to adjust anything to use it in a .htaccess file but I doubt it. If adding to a conf file don't forget to restart apache to reload the configuration.
If you are like me and want to use SSL only on particular pages then you also want a rewrite rule that sends you back to regular http for the rest. You can use the following for the reverse effect:
RewriteCond %{SERVER_PORT} ^443$
RewriteRule \.(gif|jpg|jpeg|jpe|png|css|js)$ - [S=1]
RewriteRule !^(checkout|login)(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R]
If you are using Plesk like I am keep in mind that all non-SSL traffic uses the vhost.conf file but all SSL traffic uses the vhost_ssl.conf file. That means your first rewrite rule to require SSL would go in the vhost.conf file but the second rule to force back to non-SSL will have to go in the vhost_ssl file. If you are using httpd.conf or .htaccess I think you can put them both in the same place.
I've also posted this tutorial on my blog: Apache rewrite rules to force secure/non-secure pages.
You can do this with mod_rewrite -
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/signup https://example.com/signup
RewriteRule ^/signup/(.*)$ https://example.com/signup/$1
Should work, though I haven't tested it.
-- edit --
Correction, I just tried this on one of my servers, and it works fine for me. You may want to doublecheck your mod_rewrite configuration. Also, if you're using .htaccess, you'll want to make sure overrides are allowed for that directory.
As a side note, this assumes your SSL traffic is coming over port 443. If it isn't, you'll need to adjust the rewrite condition accordingly.
.htaccess files are normally placed in a scope with Options -FollowSymLinks, which blocks Rewrite rules. This is often a security rule.
So a more trivial thing is often needed like this one:
<If "%{HTTPS} != 'on'">
Redirect 301 /your/path https://www.example.com/your/path
</If>
This is a small enhancement to the answer of Greg Hewgill.