htaccess, www redirect fake subdirectory - .htaccess

Common problem, but too complicated for me.
Here are the requirements I am trying to meet:
The root URL http://example.com should be redirected to http://www.example.com
All URLs like http://example.com/c/1234567890 should be redirected to http://www.example.com/c/1234567890 (notice the "c" fake subdirectory)
When entered http://example.com/index.php one should be redirected to http://www.example.com (no trailing slashes)
On top of that I'm trying but failing to secure a subfolder "xy" from direct access exept from php files in root and javascript files.
I searched a lot and tried a lot of rewrite conditions and rules, but htaccess+regex is just from another planet for me. :/
Sorry if this is a duplicate…

These would be in the htaccess file in your document root. Turn on the rewrite engine
RewriteEngine On
For 1 and 2: redirect to "www" if root request or request for /c/(something)
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(c/.+)?$ http://www.example.com%{REQUEST_URI} [L,R=301]
For 3: redirect direct requests for /index.php to just /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^ / [L,R=301]
For 4: forbid direct access to anything in the /xy/ directory.
RewriteCond %{HTTP_REFERER} !http://(www\.)?example\.com/ [NC]
RewriteRule ^xy/ - [L,F]

Related

.htaccess rewrite to same alias without infinite redirects

I have...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
So, /in --is-really--> /login.php
This much works great. We all can learn how to do this from: .htaccess redirect with alias url
But, I want it to also work in reverse...
If someone should enter /login.php into the address bar, I want it to change to /in.
So also, /login.php --rewrites-to--> /in
From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
That also works great.
But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...
| .htaccess : (v3) —not working!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...but then /in and /login.php both cause an infinite redirect loop.
What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?
These Questions did not help:
Rewrite rule to hide folder, doesn't work right without trailing slash
This is not about a trailing slash
Allow multiple IPs to access Wordpress Site Admin via .htaccess
This is not about IP-based access
Htaccess URLs redirects are working for http not all https
This is not about https vs http
Rewrite-rules issues : .htaccess
This is not about cleaning up the GET array in the URL
apache htaccess rewrite with alias
This is not about rewriting the host/domain, thereby preserving the path
rewrite htaccess causes infinite loop?
This is not about www subdomain rewrites
.htaccess rewrite page with alias
This is not about rewriting "pretty" URLs nor about how to use slug settings in WordPress
Htaccess alias or rewrite confusion
This is not about simply having multiple rules with the same destination
htaccess rewrite to include #!
I'm not trying to rewrite #!
Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.
Suggested .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.
Thanks to the help from the user with the correct answer, I found that...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...doesn't go in .htaccess only once, but every time on the line before...
RewriteCond %{REQUEST_URI} ...

How can I make a htaccess redirect without keeping the path?

I am really confused... I have searched and experimented, but can't seem to get anything to work. So I gave up and asked here :)
How can I redirect /subfolder to root, but every page inside the subfolder should be redirected to root?
/subfolder redirects to root
/subfolder/another-folder redirects to root (not root/another-folder)
Thanks.
To redirect requests from /subfolder to / (to the root folder) ,you can use the following Rule in your htaccess file:
RewriteEngine on
RewriteCond %{THE_REQUEST} /subfolder/(.*)\sHTTP [NC]
RewriteRule ^.+$ /%1 [NE,L,R]
The RewriteRule above will redirect any request from /subfolder/foobar to /foobar but you will get a 404 not found error since /foobar doesn't exist in your /root fodder.
To solve this ,you need a RewriteRule to internally rewrite requests from /root to /subfolder .The following rule will Internally map http://example.com/foobar to http://example.com/subfolder/foobar .The redirection from root to the subfolder is invisible.
Add the following right bellow the subfolder to /root redirection rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /subfolder/$1 [L]
EDIT :
My apologies. I misread your question. If you want to redirect your /subfolder requests to just the root folder ,try this:
RewriteEngine on
RewriteCond %{THE_REQUEST} /subfolder/(.*)\sHTTP [NC]
RewriteRule ^.+$ / [NE,L,R]

Redirect subdomain/subfolder to root domain/subfolder - But only in SOME instances

When I set up my subdomain, I created some links incorrectly. Now Google thinks I have some pages on both my subdomain and my root domain. I need to fix this, but I can't redirect the entire subdomain.
Examples of what I want to do:
https://sub.example.com/ (no redirect)
https://sub.example.com/keep-1 (no redirect)
https://sub.example.com/keep-2 (no redirect)
https://sub.example.com/move-1/* => https://example.com/move-1/*
https://sub.example.com/move-2/* => https://example.com/move-2/*
I've tried a number of .htaccess solutions and I'm close, but I can't figure it out. Here's what I've tried:
Attempt #1 - Correctly redirects, but doesn't work as a solution because it redirects everything from the subdomain
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,NE]
Attempt #2 - Doesn't redirect anything - Seems like the right solution but I'm missing something about how redirects work, I think...
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/ [NC]
RewriteRule ^(.*)$ https://example.com/move-1/$1 [L,R=301,NE]
Attempt #3 - Doesn't redirect anything
RewriteCond %{HTTP_HOST} ^sub\.example\.com/move-1/(.*)$ [NC]
RewriteRule https://example.com/move-1/$1 [L,R=301,NE]
Attempt #4 - Doesn't redirect anything
RewriteBase /
RewriteRule ^sub\.example\.com/move-1/(.*)$ https://example.com/move-1/$1 [R=301]
My .htaccess file is in the root domain's root html folder and seems to have control. I have also tried these from the subdomain's root folder, but that didn't redirect anything.
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule ^move(.*) https://example.com/move$1 [R=301,L]
%{HTTP_HOST} is the host name, mapped to domain such as sub.example.com or example.com. It does not contain any path part, that follows behind the domain. $1 is back-reference, mapped to the regex part (.*). The RewriteRule tells if the request uri pattern starts with /move, then redirect to https://example.com/move$1 permanently.

.htaccess removing www. AND redirect

I have been trying to redirect old pages for my old site to the corresponding new ones with permanent redirect.
as well as redirecting www.example.com to example.com
I dont seem to able to do both on the same time
at the moment redirects works for correct links from ex www.example.com/correctlink to example.com/correctlink
but only example.com/Info.aspx is redirected to example.com/about-magento-demo-store and NOT www.example.com/Info.aspx
*Update
I want to remove www. AND redirect 40-50 specific adress to new specific adresses. My problem is that the redirect only works if google has saved the old link without Www. IF google has stored a link including www. then my redirect Redirect permanent example.com/tabid/61/CategoryID/13/ProductID/64/Default.aspx /index.php/solpaneler/re does not function –
*Update
my htaccess looks a bit like this
(with a few lines in the end that where present before I started editing)
(i also tried to ad a line Redirect permanent http://www.example.com/Info.aspx http://example.com/about-magento-demo-store but it does not function)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.se/$1 [R=301,QSA,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
Redirect permanent /Info.aspx /about-magento-demo-store
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule .* index.php [L]
Just use rewrite rules for everything. Rewrite rules and Redirect don't always mix well.
RewriteRule ^Info\.aspx$ /about-magento-demo-store [L,R=301]
I've had the same problem a looooooooong time ago. Here's a sample of my rewriterules:
# If domain name doesn't end with .com redirect to .com:
RewriteCond %{HTTP_HOST} (.*)\.(fr|net|org|eu) [NC]
RewriteRule (.*) http://%1.com$1 [R=301,L]
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# Ca signifie forcément que c'est sans www => forcer redirection :
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]
NB: Put this on the top of your rewrite rules, because it should be processed before anything else so that you are sure your domain name always begin with "www"
Note that it redirects mydomainname only if it's "empty" i.e. nothing behind. It won't touch URLs like http://abc.mydomainname.com and it won't touch URLs like http://abc.def.mydomainname.com
Tell me if it works.

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