htaccess redirect exception folder - .htaccess

I am needing to forward a whole domain minus one folder and it's contents.
http://www.domain.com/folder_I_need/content_I_need.stuff
The rest of the site needs to forward.
Here is what I've tried:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder_I_need/$
Redirect 301 / http://www.new_domain.org/

The RewriteCond directive is part of mod_rewrite and Redirect directive part of mod_alias, they don't work in conjunction with each other. You also probably don't want the $ at the end of your condition's pattern. Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/folder_I_need/
RewriteRule ^(.*)$ http://www.new_domain.org/$1 [L,R=301]

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 to redirect a URL wildcard using htaccess

My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.

How to redirect all subpages using htaccess and exclude specific URLs?

I am trying to redirect using htaccess, but I do not know much about htaccess and redirection. What I want to do is:
redirect access to all subpages and directories e.g.
http://mydomain.com/index.php
http://mydomain.com/directory/
http://mydomain.com/new/directory/
to a specific URL on my domain say:
http://mydomain.com/one
I try to do it with
redirect /index.php http://mydomain.com/demo/
But my question is how to EXCLUDE specific URLs and directories from this redirection? How can I make rule to exclude specific pages from being redirected?
Any help is appreciated.
Your "tool of choice" should be RewriteCond
The RewriteCond directive can be used to filter more than just a URL, and each condition only applies to the next RewriteRule directive - you can "stack" RewriteCond directives, however.
Example:
RewriteCond %{REQUEST_URI} !^/path/to/non/redirect/url.php$
RewriteCond %{REQUEST_URI} !^/path/to/another/non/redirect/url.php$
RewriteRule ^$ http://mydomain.com/one
However, this can be simplified a litte:
RewriteCond %{REQUEST_URI} !^(/path/to/non/redirect/url.php|/path/to/another/non/redirect/url.php)$
RewriteRule /index.php http://mydomain.com/one
Apache Mod_Rewrite Documentation

.htaccess - all pages except homepage to load with https

I have redirected all pages to use https using the following htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Is there a way I can exclude the homepage from this? So everything apart from www.mydomain.com and mydomain.com load using https?
You may try adding this line before RewriteRule:
RewriteCond %{REQUEST_URI} !^/$
Edit
If the above doesn't work, try the following:
RewriteCond %{REQUEST_URI} !^/(index\.php|)$
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L]
Notes:
I assume you're running a PHP application server, thus there is an index.php file in the document root that will be served when someone requests /; otherwise, replace index\.php with your DirectoryIndex file (e.g. index\.html, don't forget the backslash to escape the dot)
I added a [L] (last) flag to the rewrite directive to make the rewrite engine stop here in the rewriting process, when it finds a match. To learn more, see the Rewriterule reference (Apache 2.0 link)

Redirect all pages except one page to sub-domain htaccess

I have a index.php in my main domain root
domain.com/index.php
And ive moved my forums which was in the same "root" to a subdomain
forums.domain.com
I need to Redirect everything except the index.php
ive tryed loads of ways and none seem to work
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !index\.php$
RewriteRule ^(.*)$ http://forums.domain.com [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} animelon\.com [NC]
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.*)$ http://forums.domain.com/$1 [R=301,L]
If anyone has any ideas that would be great
as for the above codes I would them googling about.
Cheers
You may use RedirectMatch instead of rewriting, that is, replace all the rewrite block you are showing with:
RedirectMatch ^(/(?!index\.php).*) http://forums.domain.com$1
You can see the full explanation of the regex on Regexr here. In brief, it sends all the URIs NOT beginning with /index.php to forums.domain.com.
If you don't need any other rewrite rule, you can turn off rewriting by removing all the lines beginning with "Rewrite" from your .htaccess.

Resources