Rewrite all requests from a single directory to a script - .htaccess - .htaccess

I need change files url in one folder in my server. It should prevent from direct download but I don't need deny from all rule.
Example:
typical direct link:
http://www.domain.com/foder1/folder2/file.ext
and I need this:
http://www.domain.com/page.html?file=foder1/folder2/file.ext
so in my case I need add this string (page.html?file=) to url. Maybe I'm wrong But I think it must be redirect becouse rewrite not execute page.html just only change (view of) url.

To do this, you need to use mode_rewrite. It will execute the page.html file just fine. It just won't update the URL in the browser bar. So, the user will have no idea they have been directed from the actual file to a script/html page. (As they probably should not!)
Here is an example:
RewriteEngine On
RewriteRule !^foder1/folder2(/|$) page.html?file=%{REQUEST_URI} [L]

Related

.htaccess redirect to page within folder leads to repeating url segments

I'd like to do the reverse of this issue. I am trying to redirect users from a subfolder to a child page. This is my current rule that doesn't work:
Redirect 302 /boston /boston/info
Using this, if I go to example.com/boston, I get redirected to example.com/boston/info/info/info/info/info/info... and it keeps going.
I imagine I'll need to switch to a RewriteRule in order to prevent this duplication. How should I go about this?
"/boston/info" - This should be the full URL you are redirecting too.
eg. www.website.com/boston/info

hide image path possible with htaccess?

I got image path like that http://blablablabla.com/Admin/img/blablabla.jpg but i do not want to show this path user can i hide this URL with htaccess ?
For example : user will see path like that http://blablablabla.com/Info/img/blablabla.jpg but i have not Info folder this is possible ? so i do not want to see users never see my images paths is way is useful or not i'm not sure tell your ideas which way to most secure to hide my path
Rewriting your url will not make a difference security-wise. If the only security you have is hoping that no-one will ever know that a certain folder exists, then you have no security at all.
The requested rewrite can be done by placing the following directives in a .htaccess file in the www-root of your website.
RewriteEngine on
RewriteRule ^Info/img/([^/]+)$ Admin/img/$1 [L]
This is an internal rewrite that rewrites the url that is requested to an internal url that points to an actual file. $1 is replaced by the first capture group. See the documentation for more information.

What to do with dynamic url after rewrite?

After you've been able to successfully create a url rewrite how do you handle the original and other possibly ways to access a page. This of course to prevent duplicate content. For example if I have this:
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(\d+)/([\w\-/\.]+)/?$ blog.php?id=$1&article_title=$2 [L]
I'm able to access the page by the url
https://www.mysite.com/blog/10/mysite.com (the mysite.com is the article title)
The problem is I'm also able to access the site by going to
https://www.mysite.com/blog.php?id=10article_title=sitetitle
https://www.mysite.com/blog.php?id=10
ect.
How are you supposed to handle those particular urls.
Also should I change the blog.php?id=10 to the rewritten url? Can I rely on something else and just start using the full rewritten url now? The site is new.
For my web site, I have the script that gets called from inside the rewrite detect the URI they were fetched from (using the "REQUEST_URI" variable that at least Apache sets), and redirect to the canonical one if they ever get called with the internal one (outputting a 301 direction).

.htaccess invisible redirect to page within subfolder

I've tried many options in .htaccess without success. What I want to do is this:
I have Wordpress residing in its own folder, like this: site.com/folder
I want all users visiting site.com to be redirected to site.com/folder/page but in such a way that the URL doesn't change in the browser. All other URLs, such as site.com/folder and site.com/folder/page1 etc shouldn't be affected.
How to do this, in as SEO-friendly way as possible?
Thanks everyone.
You can't redirect a user to a other page from serverside without to show this to the use.
But if you need, you can use the HTML tag <frame> http://www.w3schools.com/tags/tag_frame.asp
But the bad side with this is the use always will be show "site.com" and not the real page.
But I think the best way is to proxy the result, in Apache you can do this by something like this.
RewriteEngine ON
RewriteRule ^/$ folder/page/index.php [L]

Rewrite rules to hide an url

i need to hide full path and show shortly:
www.site.com/this/is/path/to/hide/page.php --> www.site.com
Any idea to do it with .htaccess apache and rewrite rules??
EXAMPLE:
If i type www.site.com i want open index.php (in /),
but if i go to /hidden/path i want to open file.php (in hidden/path)
mantaining browser url in www.site.com.
EDIT:
i want to see in the browser bar www.site.com and i want to open page at /this/is/path/to/hide/page.php .
thanks
As I explained in : How does url rewrite works? every rewrite triggers a new call to the rewritten URL. (HTTP 3xx code).
So the client will ask for www.site.com/this/is/path/to/hide/page.php, would be redirected to www.site.com and will be served the index page as a normal user.
There is no way to tell the client to display one URL in the browser bar instead of another, client browser will always make a new request. (Or you could impersonate any site for example)
If you are not against the use of a cookie, or can use environment variable you may be able to do something like :
RewriteRule this/is/path/to/hide/page.php / [co:knowHiddenPath=true]
The environment variable as same syntax with E instead of co.
(See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for cookie information)
Your index page should then check this cookie/variable to serve the hidden page or not.
Another solution would be to enable access with password to your file. So even if someone know the URL he would not access the file. Security by obscurity is does not exists.
You can I believe do that with an Alias,
Alias / /this/is/path/to/hide/page.php
This directive needs to be in your <VirtualHost>
This will use mod_rewrite and you can put this into your .htaccess
# Activate Rewrite Engine
RewriteEngine On
# Home page rewrite rule
RewriteRule ^$ /this/is/path/to/hide/page.php [QSA,L]
This will ONLY work if you hit website root (e.g. http://www.example.com/)

Resources