Adding directory to url with htaccess & rewrite - apache

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]

Related

.htaccess https to http redirect conflict with existing rule

Currently we have this rule in .htaccess
RewriteEngine on <br>
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]
What this does is for any visit we will redirect to subfolder call site if the page not found. And when this happen the url will not show site as subfolder.
Example if we have
/rootFolder/site/temp.html this will show in url as
http://www.domain.com/temp.html
This is working fine but now we need to add https redirect if user visit site.
This is the new rule I came up with
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://%{HTTP_HOST}/site/$1
RewriteRule ^(/)?$ https://%{HTTP_HOST}/site/index [L]
But the issue with this one is now url will show site subfolder
https://www.domain.com/site/temp.html
How can I achieve so that if user does
http://www.domain.com/temp.html it will find temp.html in site subfolder and redirect to https and url will only show
https://www.domain.com/temp.html
Thanks
How about if you do it in two passes, always redirecting whatever was requested to https, then running your rewrite rules, like so:
RewriteCond %{HTTPS} off
#301 flag redirects instead of rewriting
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#leaving your original rules as is
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]

HTACCESS - Allow access to folders

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.

Merge addition of URL rewriting of sub-domain and single page to capture all URLs

I have presently setup .htaccess according to the answer given here to capture all URLs in a single file. Now I want to capture all URLs having no "www" subdomain and redirect to www.my-site.com.
So, presently I have (to capture all URLs in a single file):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
And I want to merge the following with it (capture all URLs having no "www" subdomain and redirect to www.my-site.com):
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
First the redirection, and after the rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

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]