htaccess to subdirectory without changing address bar URL? - .htaccess

I've a problem with htaccess and none of the suggested solution wont work. So I hope that someone of you has the right solution for me, please.
My application is installed in a subfolder hierarchy and I try to get /dist/WSCM2 as the root directory which is also shown in the address bar of the browser.
With my current htaccess I can access my site, but the redirection is shown in the address bar, too
RewriteCond $1 !^dist/WSCM2($|/)
RewriteRule ^(.*)/?$ /dist/WSCM2/ [L]
How can I prevent showing the subdirectory?
Thx for your help!

Try with:
RewriteEngine on
# external redirect with subfolder
RewriteCond %{THE_REQUEST} \s/+dist/WSCM2(\s|/)
RewriteRule ^dist/WSCM2(/.*)? $1 [R=301,L,NE]
# internal rewrite
RewriteCond %{REQUEST_URI} !^/dist/WSCM2($|/)
RewriteRule ^(.*?)/?$ /dist/WSCM2/$1 [L]

Related

.htaccess redirect all request to subfolder only if not specifed ip

I cant figure out how to redirect all requests except my ip to a subfolder in .htaccess
I came up with
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule ^$ /coming-soon/ [L]
RewriteRule (.*) http://example.com/coming-soon [R=301]
But when i write in browser for example example.com/asdasdas it gives me
coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon
What i would like to achieve is when user enters anything that it redirects to example.com/coming-soon unless it is my IP. I did research on SO, but I always get stuck.
Also there is a folder in the root /coming-soon with images and fonts for that html
Any help would be appreciated
You can use this :
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteCond %{REQUEST_URI} !^/coming-soon [NC]
RewriteRule (.*) http://example.com/coming-soon [R=301,L]
Make sure to clear your browser cache or use a different browser to test this code.

How to redirect from folder to image in .htaccess without changing url

I have this domain sub.example.com.
And I want to redirect to an image in a subfolder without changing the url. Like this:
sub.example.com/34 ==> sub.example.com/images/34.png # sub.example.com/34 is url but an image is on the screen
I tried this:
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://sub.example.com/images/$1.png
But this just becomes an infinity loop, because it's redirecting again and again. And it also doesn't use $1 (number behind whole domain).
Thank you in advanced :)
You may use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ images/$1.png [L]

Using .htaccess to change directory in url

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.
Using answers from:
https://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess
rewrite a folder name using .htaccess
Replace directory name in url with another name
I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]
the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding
RewriteCond %{THE_REQUEST} ^GET\ /blog/
before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/
I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.
When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
I have tried adding my RewriteRule in this file but still no joy.
The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).
RewriteRule ^blog/(.*)$ /services/$1 [L,R]
And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back
RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]
But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.
Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.
And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"
If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

.htaccess RewriteRule issue

I have a domain at OVH that links to my EC2 instance like this:
www.mysite.com --> 12.34.56.78/folder/
So everytime I type "www.mysite.com" in my address bar I end up on my website but the URL has been replaced to "12.34.56.78/folder/".
I'm currently trying to tweak a .htaccess file at the root of my server but it doesn't seem to work...
Here is the content of the file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/ [L]
Maybe I'm not looking at the right solution... Anyway if you can help me, I'll be grateful!
Cheers,
You are missing a couple of things that I added below
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
#$1 will include the original URI in the redirect, 301 for permanent
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

How to redirects all request to www directory via .htaccess?

I'm trying to create a .htaccess that would for a subdomain "test" redirects all requests this way:
http://test.mydomain.com/something/something2 (1)
to
http://test.mydomain.com/www/something/something2 (2)
so that in browser's address bar is still the address (1)?
I have Apache 2.0.
I'm trying to write it for two hours and I can't still find the right way.
Thanks!
You'll want something like this:
RewriteEngine On
# Only redirect if we're on the subdomain and haven't redirected
# (internally) to /www/ yet
RewriteCond %{HTTP_HOST} ^test
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^.*$ /www/$0 [L]
# Let's also prevent them from being able to go to our /www path manually
RewriteCond %{THE_REQUEST} ^(POST|GET)\s/www
RewriteRule ^www/?(.*)$ http://test.mydomain.com/$1 [R,L]

Resources