htaccess mod_rewrite .tld to specific folder - .htaccess

I want to do the following, if possible, with htaccess.
The webpage works this way, you get the index file via domain.de/de/start and the english version via domain.de/en/start.
If a user visits domain.com I would like him to end up at domain.de/en/ instead of domain.de/de/ so I need to rewrite all requests to domain.com but NOT requests with domain.com/xx/something to domain.de/en/
Thanks.

If I understand you clearly you only want to redirect http://domain.com/ to http://domain.de/en/ but do NOT want to redirect http://domain.com/XX/YY
Put this code in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^$ http://domain.de/en/ [L,R=301]

Try to use HTTP_HOST and rewritecond
RewriteCond %{HTTP_HOST}=domain.de
RewriteCond %{REQUEST_URI}=/ % redirect only the root
RewriteRule ^/$ /de/start [L] % to /de/start
RewriteCond %{HTTP_HOST}=domain.de
RewriteCond !%{REQUEST_URI}=/ % no root
RewriteCond !%{REQUEST_URI}=/[a-z]{2}/.* % except /xx/ directories
RewriteRule ^/.*$ /de/$1 [L] % push the user in the /de/ directory

Related

Redirect Specific Pages Then Redirect All Others

Where to start.
I need to move a website to a new subdomain with around 20 specific pages, and then just blank redirect everything else.
Any ideas?
You'll want something like this in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# Redirect specific pages
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(index\.html)$ http://newdomain.com/path/$1 [R=301,L,NC]
RewriteRule ^(index2\.html)$ http://newdomain.com/path/$1 [R=301,L,NC]
RewriteRule ^(index3\.html)$ http://newdomain.com/path/$1 [R=301,L,NC]
# Redirect all others to the new domain
RewriteRule ^(.*)$ http://newdomain.com/ [R=301,L]
Customize and test for your situation using: http://htaccess.madewithlove.be/

URL rewrite to remove trailing folder name

We have a server, and have several folders under the /var/www/ folder.
We have pointed our domain name to the IP of the server, and by default we expect to load one folder as the website (since its not possible to point a folder with DNS).
I have written .htaccess in such a way that when you enter the IP or the domain name, the request redirects to the website folder.
However, whenever we enter the IP or the domain name, the name of the folder is getting added to the URL.
Here is the present .htaccess:
Options +FollowSymlinks -Multiviews
#DirectoryIndex folder/
RewriteEngine on
ReWriteBase /
RewriteRule ^$ /folder [L]
RewriteRule ^$ folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
where the folder is the folder's website
so,
www.domain.com
becomes
www.domain.com/folder/
Is there a way to rewrite the URL to remove the folder name?
Thanks in advance :)
EDIT : Added .htaccess code
Have your rule like this in DocumentRoot/.htacess:
DirectorySlash On
Options +FollowSymlinks -MultiViews
RewriteEngine on
ReWriteBase /
# redirect /folder/abc123 to /abc123
RewriteCond %{THE_REQUEST} \s/+folder/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# skip rewrites for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule !^(index\.)?$ - [L]
# route everything internally to /folder
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]
It sounds like you made an external redirect instead of an internal rewrite. An external redirect is denoted by the [R] flag (with optional parameter) and causes Apache to send a 301 or 302 header back to the client with a different url that client should request. This causes the client to show the new url in the address bar.
What you want is an internal rewrite. When you request the url, the url is internally rewritten to the place where the resource is actually located. You do this by omitting the [R] flag, and not using a domain name in the rewritten part. It typically looks something like this:
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ /folder/$1 [L]

htaccess redirect subfolder

I would like to redirect the url http://intranet/trac/paradox/report/6 to http://cobra.woking/trac/paradox/report/6. trac is a subfolder and paradox is a subfolder. report/6 are params that need to be kept and may change.
In my apache doc root i have
#/opt/html/.htaccess
Redirect 301 / http://intranet/intranet
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]
I have tried the following which does not work
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
URL i want to change is http://intranet/trac/paradox/ to http://cobra.woking/trac/paradox/. I have placed .htaccess in the /opt/html/trac/paradox/.htaccess
In the htaccess file of your intranet's document root, add this to the top of the file:
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]

Redirect root (and only root) URL to another domain?

I'm looking to redirect my domain example.com and www.example.com to http://example2.com, but I want to keep all visits to my posts at http://example.com/sdfhs to continue through to their destinations.
Essentially, I ONLY want to redirect the root and www domain and leave the rest untouched.
How can I do this with .htaccess?
Don't know if you still have this issue, but I have had it on one of my domains (Yourls installation). Try:
RewriteEngine On
RewriteRule ^$ http://www.example.com/ [R=301,L]
It works on my installation, but check it out on your site.
Why not
RewriteEngine On
RewriteCond ${HTTP_HOST} example\.com|www\.example\.com
RewriteRule ^/?$ http://example2.com [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond ${HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://example2.com [L,R=301]
You could simply match any URL with at least 1 character (.+) and mark that as the last rule for the rewrite engine to look at [L].
Then afterwards redirect anything else (.*) to the new domain (the only other thing that wouldn't be matched up to that point would be the root).
RewriteEngine On
RewriteRule ^(.+)$ - [L]
RewriteRule ^(.*)$ http://example2.com/$1 [R=301,L]

Problem 301 redirect not allowing login in the backend

I am finding some problems in the htaccess of CMS with a 301 redirect.
When trying to solve canonical urls (redirecting site to www.site) I got the problem that I cannot log in in the back end (www.site/admin).
The htaccess condition is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.site\.co.uk$
RewriteRule (.*) http://www.site.co.uk$1 [R=301,L]
I guess I need to include a expression that allows the URI /admin not to be redirected, but how?
Like this, for example:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.co.uk
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule .* http://www.example.co.uk%{REQUEST_URI} [R=301,L]

Resources