HTACCESS - Allow access to folders - apache

This is my current htaccess.
Site
|.htaccess
|--/folder-1
|--/folder-2
Now my root htaccess is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$
RewriteRule ^/?$ /public/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*) /public/$1 [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I want users to access all files and folders inside folder 1 and folder 2. How that can be done?

Several changes: do your domain redirect first (note the subtle changes);
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!(?:folder-1|folder-2)(?:$|/)|public/(?:index\.php)?$).* public/$0 [L]
This incorporates the folders you're excluding from rewriting into the main rule, so should be more efficient. It looks ugly since it starts with a negative look-ahead assertion (?! and the rest of the groups begin with (?: so they are non-capturing.
Do you really need a single domain? If so, it should be checked on all requests. If not, drop the first RewriteCond of the second RewriteRule.
if your default document is not index.php, change it in the negative look-ahead assertion of the second RewriteRule.

Related

Adding directory to url with htaccess & rewrite

I have a site based on Modx CMS and I need to create a 301 redirect for the main folder & old links after the multilanguage functionality has been enabled.
What I need to achieve is the default fallback to english eg. make sure that:
example.com/products -> example.com/en/products
example.com/about - example.com/en/about
etc.
I also need to make sure that if there already is a language selection (for example de) in the url, I don't add en to url. (so no example.com/en/de/products)
I am having trouble adding the /en/ to url and I am ending up with infite /en/en/en loops on the URL
To add the /en/ to the url I tried the following.
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
It results to a endless /en/en/en loop in the url.
The whole htaccess is as follow:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Try the code below. The rules are chained (the default is AND). So, if your URL doesn't start with /en, /de, it's not the index (/), and it's not a file name or a directory, do a 301 redirect to the English version:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_URI} !^/de
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/en%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Apache - .HTACCESS applies previous conditions to current rewrite url

My .htaccess applies RewriteCond's from previous RewriteRule's to the current RewriteRule.
For example example.com/acme gets redirected to http://www.example.com/index.php?url=acme.
Why would this configuration ever redirect to index.php? The index.php's RewriteRule isn't even a R=301. And the RewriteCond's that makes exceptions when the request_uri contains the string acme shouldn't even be applying themselves to this RewriteRule since they have their own RewriteRule.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(txt) [OR]
RewriteCond %{REQUEST_URI} robots\.txt
RewriteCond %{REQUEST_URI} !acme
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !\.(txt) [OR]
RewriteCond %{REQUEST_URI} robots\.txt
RewriteCond %{HTTPS} !=off
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It seems lke it executed 2 rewrite rules at the same time. Adding the [END] flag fixed the issue.

.htaccess with multiple rewrite rules, based on path

I have the following .htaccess, which is not working as expected:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login.* [OR]
RewriteCond %{REQUEST_URI} ^/admin.*$
RewriteRule ^(.*)$ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login.*$
RewriteCond %{REQUEST_URI} !^/admin.*$
RewriteRule ^(.*)$ perla/$1 [L,NC]
What I need is the following:
If the URI is missing the "www" from example.bg, then add it and redirect to www.example.bg
If the URI does not end in "sessiontest.php" and starts with "www.example.bg/login" or starts with www.example.bg/admin, then forward all requets to travelpartner.php
For all other cases, we should forward all requests to a subfolder, called "perla"
However, right now what happens is that we always see the contents of the perla subfolder, even if I open exemple.bg/login
What am I missing here?
With your current rules I get the infinite redirect loop:
Once the rule No.2 is matched, and the request /login is rewritten to /travelpartner.php, the rewritten request is handed back to the URL parsing engine and the ruleset is run again from the start.
This time, it will match the last segment and be rewritten to /perla/travelpartner.php,and sent back again, and be rewritten to /perla/perla/travelpartner.php, etc...
The fix is to, in the last segment, prevent rewriting if the request starts with /perla or is /travelpartner.php:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/perla/
RewriteCond %{REQUEST_URI} !^/travelpartner\.php$
RewriteRule ^(.*)$ perla/$1 [L,NC]
You can use the following rules
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteRule ^(login|admin) /travelpartner.php [L]
RewriteRule !(perla/|travelpartner\.php) /perla [L]

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

Mod_rewrite not redirecting all cases correctly

I always seem to have trouble with this and no matter what I try I can't get all cases to redirect correctly.
I currently have
example.com/anything
correctly redirects to
example.com/track.php?memb=anything
and basically any url with a filetype at the end should ignore all redirects. I also have a subdomain rule at the top which I think is implemented correctly to not interfere with what I am trying to add. All of this is currently working correctly with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)? [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* track.php?memb=%1 [L,E=END]
I need to add the following rule:
example.com/c/whatever
needs to redirect to
example.com/page2.php?c=whatever
After many attempts and modifications of some of the current rules, I can't seem to get it working but I think I'm close with
RewriteRule ^c/(.*)$ page2.php?c=$1 [L,E=END]
RewriteCond is only valid for the next RewriteRule. Also,
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)? [NC]
will match anything with a . in the name. Since you have a ? at the end, it won't matter if the extension has a valid name or not.
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$ [NC]
RewriteRule ^c/([^/]+)/? page2.php?c=$1 [L,E=END]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$ [NC]
RewriteRule ^([^/]+)/? track.php?memb=$1 [L,E=END]