Apache URL Rewriting - Hide path in URL only when accessing specific path - apache

I would like to rewrite my URL only when typing this:
www.domain.com/dir1/dir2/dir3/
in that path there is a index.html file that will be automatically loaded. And I'd like it to become this:
www.domain.com/index.html
It is important that the root www.domain.com does not redirect to that subdirs because it already redirects to another .php file.
I wrote the following .htaccess file:
RewriteRule ^dir1/dir2/dir3/(.*)$ dir1/dir2/dir3/$1 [L]
but it redirects even when typing the root domain.com.
Could you help me with that?
Edit:
I have tried in another browser and the real problem is that when typing domain.com/dir1/dir2/dir3 or domain.com/dir1/dir2/dir3/index.html the dir1/dir2/dir3/ is still shown and is not hidden.
Edit 2:
my complete .htaccess:
AddHandler application/x-httpd-php56s .php
ErrorDocument 404 /404/404.html
ErrorDocument 500 /404/404.html
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404/404.html [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404/404.html [R=500,NC,L]
Thanks

Replace your current code with this:
DirectorySlash On
RewriteEngine On
# skip all known resources from rewrite rules below
RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+dir1/dir2/dir3/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(?!dir1/dir2/dir3/)(.*)$ dir1/dir2/dir3/$1 [L,NC]

Related

Block Access to Entire Folder and Instead Route to index.php

I'm trying to write a .htaccess rewrite rule to redirect all traffic coming to domain.com/system/* back to domain.com.
I've written this so far:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1/ [L]
But it's not working as expected, if I go to:
domain.com/system/inexistent_file
it works. But if I go to:
domain.com/system/existing_file.php
It will open the specified file instead of showing index.php contents.
How should I fix this?
Try this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
# Match against the host, if you wish
#RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule system/(.*) /index.php?/$1 [L,R]
</IfModule>
It works for both existent and non-existent files.
The [R] flag is there because you stated that you want to redirect.

Index redirects me to a different file

Wasnt sure what to put in the title to describe what is going on, but I was looking at ways I could deny direct access to files and folders. I added a line in .htaccess which didnt work so I removed it.
I have access to every page on my website, but index redirects me to the page I tried to prevent direct access to. I have deleted the content of .htaccess, index and the page im been redirected to, restarted wamp and used CTRL + F5 but nothing seem to work. No cookies are stored and no errors in inspect element.
This is my .htaccess file
Options -MultiViews +FollowSymlinks
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^http://example.com$ [NC]
#ERROR CODE -----------------------------------------------------------
ErrorDocument 400 /assets/error/400.php
ErrorDocument 401 /assets/error/401.php
ErrorDocument 403 /assets/error/403.php
ErrorDocument 404 /assets/error/404.php
ErrorDocument 500 /assets/error/500.php
RewriteRule ^400/?$ /assets/error/400.php [NC,L]
RewriteRule ^401/?$ /assets/error/401.php [NC,L]
RewriteRule ^403/?$ /assets/error/403.php [NC,L]
RewriteRule ^404/?$ /assets/error/404.php [NC,L]
RewriteRule ^500/?$ /assets/error/500.php [NC,L]
#User Pages-------------------------------------------------------
RewriteRule ^//?$ /index.php [NC,L]
RewriteRule ^signin/?$ /signin.php [NC,L]
RewriteRule ^signup/?$ /signup.php [NC,L]
RewriteRule ^signout/?$ /signout.php [NC,L]
RewriteRule ^forgot-password/?$ /forgot-password.php [NC,L]
RewriteRule ^credits/?$ /credits.php [NC,L]
RewriteRule ^leaderboard/?$ /leaderboard.php [NC,L]
RewriteRule ^updates/?$ /updates.php [NC,L]
RewriteRule ^live/?$ /live.php [NC,L]
#Misc--------------------------------------------------------------
RewriteRule ^misc/?$ /misc.php [NC,L]
RewriteRule ^maintenance/?$ /maintenance.php [NC,L]
#Maintenance-------------------------------------------------------
# files to load
# RewriteCond %{REQUEST_URI} !/assets/css/main.css$
# RewriteCond %{REQUEST_URI} !/assets/css/special.css$
# RewriteCond %{REQUEST_URI} !/maintenance.php$
# RewriteCond %{REQUEST_URI} !/.htaccess$
# here filter the developer's IP/
# RewriteCond %{REMOTE_HOST} !^212.251.187.11
# redirect page (maintenance page)
# RewriteRule $ /maintenance [R=302,L]
Edit
The problem seem to be occurring when I use example.com or example.com/, but not when using example.com/index.php.
There is only 1 .htaccess file been used and I've not made any changes to the apache files (atleast not my self), Im trying to find the .htaccess snippets I tried right before this started happening
I transfered my root folder to a different pc and everything works. I'm not sure what went wrong but I was not able to fix the problem. I'm reinstalling wamp to see if that solves the problem. I belive this is the code that I used before index stopped working Redirect 301 /old/file.html http://yourdomain.com/new/file.html I never replaced any of it with the index but with a 404 page and the page I was been redirected to.
The snippet was taken from:
http://viralpatel.net/blogs/21-very-useful-htaccess-tips-tricks/

.htaccess - Redirect to a subfolder according to the preferred language

we have a website in English. Now we want to have a translated copy of this website in German.
The index.html is located in the root folder: www.mydomain.com/index.html
Now I want to create a htaccess file which redirects all user which have set the preferred language in their browser to German (de, de-de, de-at, de-ch) to www.mydomain.com/de/index.html. All other user should be redirected to www.mydomain.com/en/index.html.
I tried a lot but I can't make it work. I do not really understand the regular expression thing and it's usage.
Most of the tries ended up with error 403 or 500. The current htaccess doesn't end up in an error, but it doesn't work.
The htaccess is now:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^/(.*)$ /de/$1 [L]
RewriteRule ^/(.*)$ /en/$1 [L]
It seems like the file doesn't work at all. When I open www.mydomain.com it opens www.mydomain.com/index.html
Htaccess and rewriting works in general:
RewriteRule ^(.*)$ http://www.google.com/ [R=301,L]
works.
If I delete the slash behind ^
RewriteRule ^(.*)$ /en/$1 [L]
I get an error 500.
What do I do wrong?
I actually want the server to redirect to the webpage which the user tried to open. So if the user opens mydomain.com/index2.html he should be redirected to mydomain.com/xx/index2.html (and not to index.html)
Any ideas?
Thanks,
Jaroslaw
You can use this rule:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]

mod_rewrite keeps adding .html for files

I have a redirect situation where the site is part dynamic and part generated .html files.
For example, mysite.com/homepage and mysite.com/products/42 are actually static html files
Whereas other URLs are dynamically generated, like mysite.com/cart
Both mysite.com and www.mysite.com are pointing to the same place. However I want to redirect all of the traffic from mysite.com to www.mysite.com.
I'm so close but I'm running into an issue where Apache is adding .html to the end of my URLs for anything where a static .html file exists - which I don't want.
I want to redirect this:
http://mysite.com/products/42
To this:
http://www.mysite.com/products/42
But Apache is making it this, instead (because 42.html is an actual html file):
http://www.mysite.com/products/42.html
I don't want that - I want it to redirect to www.mysite.com/products/42
Here's what I started with:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
I tried making the parameters and the .html optional, but the .html is still getting added on the redirect:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)?(\.html)?$ http://www.mysite.com/$1 [R=301,L]
What am I doing wrong? Really appreciate it :)
Here is the code you will need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## first add www to your domain for and hide .html extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ http://www.mysite.com%1 [R=301,L]
## add www to your domain for other URIs without .html extension
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.mysite.com%{REQUEST_URI} [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Maybe you should try looking at apache's mod_negotiation to get rid of the .html or any file extension?
Link: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html

redirect into subdirectory AND out of subdirectory

i have a mod_rewrite redirection problem i cannot figure out.
all requests from a specific domain get "silently" rewritten into a designated subdirectory. e.g. www.mydomain.net/hello.html retrieves the file in /net/hello.html. the following .htaccess (placed in my hosting root) achieves this perfectly:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]
however, direct URLs into this directory however should visibly redirect with a 301 to the URL without that subdirectory. e.g. www.mydomain.net/net/hello.html should redirect to www.mydomain.net/hello.html (which than still retrieves the file in /net/hello.html). my .htacces file for this (placed in /net) unfortunately doesn't work:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]
i get an infinitive redirect loop despite the RewriteCond %{ENV:REDIRECT_STATUS} 200 block in the root .htaccess file... so what's wrong?
btw, i have to use mod_rewrite, because the site is externaly hosted and i have no access to the apache configs.
many thanks for any pointers.
Inspect the HTTP request line in THE_REQUEST instead:
RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]