How to "change" folder name (and block original path) using htaccess? - .htaccess

i.e. I have (actual folder path):
example.com/folder1
but I want that anyone visiting that link directly was blocked (not redirecting anywhere, just deny).
Instead, I want that link was only accessed at:
example.com/dfjgekbjvrskzcnfvberidurvf
How to do that?

At your main root .htaccess put the flowing code :
RewriteEngine on
RewriteRule ^dfjgekbjvrskzcnfvberidurvf/?(.*)$ /folder1/$1 [L]
Then at .htaccess file in folder1 directory put this :
RewriteEngine on
RewriteCond %{THE_REQUEST} !dfjgekbjvrskzcnfvberidurvf [OR]
RewriteCond %{THE_REQUEST} folder1
RewriteRule ^ - [F,L]

Related

Htaccess rewrite in subfolder with parameters

I'm trying to rewrite /streams/post.php?id=1 into /streams/thread/1 .
I did find some codes for .htaccess with parameters but it didn't work in the subfolder.
Also, should I put another .htaccess in the subfolder or use the one on the main directory? ( /.htaccess or /streams/.htaccess)
Also, in the past when I've rewritten, sometimes it redirects from for example /streams/thread/1 to /streams/post.php?id= and sometimes it actually displays /streams/thread/1 in the URL, I would like it to display /streams/thread/1 in the URL, not just redirect.
You can put your htaccess in root or streams folder (that's up to you).
In root folder
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/streams/post\.php\?id=([0-9]*)\s [NC]
RewriteRule . streams/thread/%1? [R=301,L]
RewriteRule ^streams/thread/([0-9]+)$ streams/post.php?id=$1 [L]
In streams folder
RewriteEngine On
RewriteBase /streams/
RewriteCond %{THE_REQUEST} \s/streams/post\.php\?id=([0-9]*)\s [NC]
RewriteRule . thread/%1? [R=301,L]
RewriteRule ^thread/([0-9]+)$ post.php?id=$1 [L]

Hidden redirect of / to subfolder using .htaccess in TYPO3

We're setting up a TYPO3 installation, and if the user calls example.com/ we'd like the server to redirect to /typo/index.php?id=106.
This should happen without a change in the address bar. Every other file access on the server (for example example.com/test.png) should be redirected to example.com/typo/test.png).
This is the .htaccess file in the root directory. As I understand, it will redirect everything which doesn't have /typo in the URL to the subfolder and attach the parameters:
RewriteCond %{REQUEST_URI} !^/typo/
RewriteRule ^(.*)$ typo/$1 [L]
Now, this already seems to work, when I call example.com/index.php?id=106 I'm not getting a 404. Unfortunately TYPO3 seems to have some trouble (or the .htaccess configuration isn't correct), because we get a message saying "No input file specified".
What's also missing is the initial redirect when no path is specified. It should then go to /typo/index.php?id=106.
You may try this in one .htaccess file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# URL with no path
RewriteCond %{REQUEST_URI} ^/?$ [NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule .* /typo/index.php?id=106 [NC,L]
# URL with path
RewriteCond %{REQUEST_URI} !^/typo [NC]
RewriteRule ^(.+) /typo/$1 [NC,L]
Maps silently:
http://domain.com/ to
http://domain.com/typo/index.php?id=106
and
http://domain.com/anything
http://domain.com/typo/anything
For permanent redirection, replace [NC,L] with [R=301,NC,L]

How to change domain.com to domain.com/folder using htaccess

I have this folder structure
webroot/
www/
srv/
When I visit http://www.domain.com, it points to the www/ folder using this codes:
RewriteCond %{HTTP:Host} ^(?:www\.)?domain\.com$
RewriteRule (.*) /www/$1 [NC,L,NS]
It shows the contents of the www/ folder without the folder name on the address bar. It retains its name to:
http://www.domain.com
What I want is to use the .htaccess mod_rewrite to change from:
http://sub.domain.com
to
http://sub.domain.com/srv
Where it shows the sub folder and it contents. Is it possible? I tried:
RewriteCond %{HTTP:Host} ^(?:sub\.domain\.com)?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/srv/$1 [R=301,L]
but it returns a redirect loop error. How can I do this?
You need to check that it isn't already pointing to /srv. Try:
RewriteCond %{HTTP:Host} ^(?:sub\.domain\.com)?$
RewriteCond %{REQUEST_URI} !^/srv/
RewriteRule ^(.*)$ http://%{HTTP_HOST}/srv/$1 [R=301,L]

htaccess rewrite subfolder to root

In my root folder I have an .htaccess file that redirects URLs to a subfolder as follows. This folder contains a coming soon page:
RewriteCond %{HTTP_HOST} ^(www.)?myotherurl.com$
RewriteRule ^(/)?$ test [L]
When this rule is applied I get the following url: http://myotherurl.com/test/
Now I would like to rewrite (not redirect) the shown URL to http://myotherurl.com by creating a nested .htaccess. What should this file contain?
Try this one:
RewriteCond %{HTTP_HOST} ^(www\.)?myotherurl.com$
RewriteRule ^/?(.*)$ test/$1 [L]
This puts everything to the target directory. In your condition you missed to mask the dot. This does not matter in this case but it would also match the domain wwwamyotheeurl.com
If you have just one static page you could also use this rule:
RewriteRule ^ test/index.html [L]

Check for a directory inside a subdirectory dynamically

I have a bunch of domains which points to the same directory "public_html" on my host.
There is a .htaccess file in the main folder that locally redirects each domain to a particular folder with the same name as %{HTTP_HOST} mod_rewrite variable. (eg: redirect www.domain.com to public_html/www.domain.com/)
This is the content of the .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L]
I'm having a problem when it comes to the Directory Slash directive.
If I try to access a folder in the domain url without the forward slash like http://www.domain.com/folder, the mod_dir applies the DirectorySlash and externally redirects my request to http://www.domain.com/www.domain.com/folder/
I tried applying a 301 redirect AFTER the domain directory redirect like this:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]
But for this to work, I would have to be able to check if the %{REQUEST_FILENAME} exists inside the %{HTTP_HOST} 'folder'. I even tried the following, for no success:
RewriteCond /%{HTTP_HOST}/%{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]
How can I check, Dynamically, if the %{REQUEST_FILENAME} is a directory, which would be inside a directory with the same name as %{HTTP_HOST}?
Thanks in advance
If you work with -d you need to provide an absolute filesystem path. So try this:
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

Resources