How to access folder files located from other domain folder located inside the public-html? - html

I want to access the image folder which is located in (e.g "example.com/images/one.jpg").
I have created the subdomain named (e.g "subdomain.example.com").
Both domain folders are in same public_html.
How i can authoize my subdomain, which can access files using htaccess rules or anything else. i tired this "../../" before file name, but not working.
Note: i dont want to put a whole path to access the files from root domain, such as https://......
Thanks

Browsers deal in URLs, not the paths on your filesystem.
It sounds like you have configured your server so that the images are only available on example.com and are not available on subdomain.example.com.
You therefore need to tell the browser to access the images from the other hostname.
src="//example.com/images/one.jpg"

If you have a domain.com and sub.domain.com, these are two completely different paths. You cannot get data from each other using reference as path like '../images/foo.png'.
In this case, you have to put all the path https://www.sub.domain.com/images/foo.png. This is the same way you need to do when uses a subdomain as resource in order to decrease the number of request in your website for images and sources in general.

I did a little bit of server configuration in the .htaccess file. I write the rule that no image can be publicly accessible. but I want to authorize my subdomain which can access the image URL.
:
Note: both domains are in the same public_html directory
You would seem to require an exception for the subdomain and block other hostnames?
It's a bit of a guess how you are currently doing this - either with mod_authz_core and a <FilesMatch> container perhaps? Or using mod_rewrite? Or an Apache <If> expression??
You could do something like the following using mod_rewrite, near the top of the root .htaccess file to block access to all images, except when accessed via the subdomain.
RewriteEngine On
# Block (403 Forbidden) all image requests unless accessed through the subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com
RewriteRule \.(jpe?g|png|webp|gif)$ - [F]
Now, a request of the form https://example.com/images/one.jpg will be blocked, but https://subdomain.example.com/images/one.jpg will be accessible.
i dont want to put a whole path to access the files from root domain
But you will need to use an absolute (or protocol-relative) URL to access the files from the subdomain.
UPDATE:
Current rules in my parent domain "example.com"
RewriteCond %{HTTP_REFERER} !^https://example.com/ [NC]
RewriteRule \.(gif|jpg|jpeg|png|mp4|mov|mkv|flv|svg)$ - [F,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ah, so you've implemented a form of hotlink protection! In that case you would seem to just need to modify the first condition to allow requests from the subdomain as well. For example:
RewriteCond %{HTTP_REFERER} !^https://(subdomain\.)?example.com/
To allow requests from both example.com and subdomain.example.com. The NC flag should be omitted. All browsers will lowercase the hostname in the request.
No other rules are required to "allow" access. You do not need to use the absolute URL to access these resources - since that does not appear to be what you are blocking. You simply need to be able to access these resources from another host (ie. the subdomain).
In summary:
RewriteEngine On
# Hotlink protection
RewriteCond %{HTTP_REFERER} !^https://(subdomain\.)?example.com/
RewriteRule \.(gif|jpg|jpeg|png|mp4|mov|mkv|flv|svg)$ - [F]
# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The L flag is not required with the F flag, since it is implied.
The ^(.*)$ is unnecessary since the backreference is not being used. Simply having ^ is sufficient and more efficient - to be successful for everything without having to actually match everything.

Related

Self Hosted Website www redirect to non www

I am self hosting a website on a Synology NAS. I have set up an SSL certificate, set up the DNS records on Google Domains. If I enter the website, andrewr.ca, into a browser it works fine. If I enter www.andrewr.ca it does not work and the SSL is not valid. I am trying to figure out how to fix this issue but get stuck every time. I have a .htaccess file that looks like this:
And my Google Domains account looks like this. [enter image description
My SSL cert looks like this:
Could someone please point me in the right direction here. I just want to be able to enter www.andrewr.ca and it works the exact same as if I enter andrewr.ca
I have tried changing the CNAME, changing the .htaccess and also adding a different SSL certificate but had no luck with anything I try so far.
This honestly probably has two separated answers out there for you to find, but I am posting this just because it's a solution we use currently in our setup to rewrite non https to https AND non www to www in one swoop. These are the rewrite directives in my production .htaccess
#SSL = 1
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^/?(.*)$ https://example.com/$1 [R=301,L]
# Redirect www and http to https - non-www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
This is not for everyone and ONLY works if you have a single domain in your setup -- Which the top two lines of the htacces file there are put there to deal with multiple domains on the single files set to canonicalize the domain .. Once you have multiple domains in there .. You WILL NEED a separated SSL for each domain, AND the www for it to work .. But as for a SINGLE domain, this will forward to https - non www without the need for a secondary SSL on the www

Redirecting multiple duplicate URLs to main URL using .htaccess

I developed a website and from SEO point of view, the homepage of a website is available under three or more different URLs which accordingly poses a risk of duplicate content, since the same content can be accessed via different URLs.
for eg.
my website is opening with below URL
https://www.insider.in/index.php
https://www.insider.in/
https://insider.in/
Exact URL which is needed be like
https://www.insider.in/
for achieving the same i am using an .htaccess file with code given below:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.(php|html?)[#?]?
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ https://insider.in/$1[R=301,L]
I tried to redirect all duplicate URLs to one URL using the above .htaccess file code but it is not working, also i enabled all the file permissions in FileZilla but again no success.
Any help or reference will be appreciated, thankyou!
note- website is hosted in cloud server using FileZilla
for Reference I am sharing the screenshots
Image of development folder for your reference
Image of .htaccess rule which i have used
You can use the rules below to fix duplicate content issues on your site :
RewriteEngine on
#Redirect non-www domain to www
RewriteCond %{HTTP_HOST} ^insider\.in$ [NC]
RewriteRule ^ https://www.insider.in%{REQUEST_URI} [NE,L,R=301]
#Redirect /index.php to /
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule ^ https://www.insider.in/ [L,R=301]
Remember to put theses rules at the top of your htaccess.

How to htaccess 301 redirect pages with a question mark in the url

I'm trying to redirect several pages that all have question marks in the URL.
I essentially want to redirect:
www.example.com/?attachment_id=456 to www.example.com
There's a ton of pages with differend id #s also.
I've tried a few things in htaccess with no luck..
Any ideas?
This is what I tried:
RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]
RewriteRule ^/$ http://www.example.com/? [L,NC,R=301]
Why can't you do this? This code should redirect a URL like this www.example.com/?attachment_id=456
RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]+
RewriteRule ^/?$ http://www.example.com/? [L,NC,R=301]
I made the / optional so that it can be used in Apache config or .htaccess. Also I kept the ? that you have in the redirect at the end of the RewriteRule to remove any query strings on redirect.
Your approach is next to perfect, just some minor corrections:
RewriteEngine on
RewriteCond %{QUERY_STRING} attachment_id=[0-9]+
RewriteRule ^/$ http://www.example.com/ [L,R=301]
The above is the version for the host configuration. note that you have to restart the http server after having made changes to the host configuration for them to get effective. To debug refer to the http servers error log file, especially at restart time.
If you have to rely on .htaccess style files, then the syntax for the rule itself must unfortunately be slightly different:
RewriteEngine on
RewriteCond %{QUERY_STRING} attachment_id=[0-9]+
RewriteRule ^$ http://www.example.com/ [L,R=301]
Such file has to be located in the main folder of the document root of the host. also the interpretation of such files must be enabled in the host configuration by means of the AllowOverride option.
In general you should always prefer the host configuration for such rules over .htaccess style files, but you need administrative access for that. .htaccess style files are notoriously error prone, hard to debug and really slow the server down.

Clean/Short URLs using .htaccess is not working as it should

I've been following some YouTube videos to try and get this working but it doesn't seem to want to. This is how my .htaccess file is looking:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule RewriteRule ^(.*)$ index.php/$l
I am completely new to .htaccess, I'm not completely sure how it works but my common sense tells me to just create a file, call it .htaccess and place it in the same folder as my index.php, right?
So when I go to, say index.php/foo/bar or even just /foo/bar/ I get a 404 error.
What are the possible problems? It's a Windows 2008 Server with PHP 5.3.10 installed.
As beginning your last line should be
RewriteRule ^(.*)$ index.php/$l
And it should work as you expect it... pass all requests but ones for existing files/folders into index.php
The .htaccess can be in any folder which is hit by the request URL... the only difference is the base of the URL which is relative to the folder where is the .htaccess located
Example:
/folder1
.htaccess
index.php
/folder2
.htaccess
index.php
If you access /test.php none if your .htaccess files will be processed (it's not related to the request URI.
If you access /folder1/test.php the corresponding .htaccess in folder1 will be used and the $1 in your RewriteRule will show test.php, the /folder1/ will be stripped from the URI.
If you access /folder1/folder2/test.php it's similar... apache will use .htaccess from folder2... the one from folder1 will be ignored (a bit counderintuitive)

Redirect request for directory-name to directory-name.html with htaccess

On my website, when somebody requests a directory, I want it to remove the '/' and add '.html' (minus the quotes, of course).
Example:
If someone goes to domain.com/directory/ it should redirect to domain.com/directory.html/ and the same should stand for: domain.com/another-directory/ should redirect to domain.com/another-directory.html.
I would like to place a line (or two) of code to my htaccess file that will make any directory (URL ending with /) redirect to URL.html (removing the / of course).
I would also like it to visually redirect, so the user will actually see it change to .html.
I'm quite a novice web programmer, and any help is greatly appreciated.
Note: I did use Redirect /directory /directory.html and that worked, but that requires a lot of extra coding, and I would much prefer one simple statement to cover all directories.
This is going to be a bit difficult with htaccess, I assume you want to do the following:
If someone accesses a directory that isn't the root (simply http://domain.com/), redirect them to the directory name ending with .html
After the get redirected, internally rewrite the .html back to the directory so apache can serve the directory.
First one is straightforward:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to make sure the request is for a directory that exists
RewriteCond %{REQUEST_FILENAME} -d
# rewrite the directory to
RewriteRule ^(.+)/$ /$1.html [R]
Second part is tricky
# check to make sure the request IS for an html file
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# See if the directory exists if you strip off the .html
RewriteCond %{DOCUMENT_ROOT}/%2 -d
# Check for an internal rewrite token that we add
RewriteCond %{QUERY_STRING} !r=n
# if no token, rewrite and add token (so that directories with index.html won't get looped)
RewriteRule ^(.+)\.html /$1/?r=n [L,QSA]
However, if what you just have is a bunch of files called directory.html, directory2.html, directory3.html, etc. and you want to make it so when someone enters http://domain.com/directory2/ into their address bar they get served the contents of directory2.html, it will be much simpler:
# check to make sure the request isn't actually for an html file
RewriteCond %{THE_REQUEST} !^([A-Z]{3,9})\ /(.+)\.html\ HTTP
# check to see if the html file exists (need to do this to strip off the trailing /)
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
# rewrite
RewriteRule ^(.+)/$ /$1.html [L]