.htaccess Redirect all pages except one and another internal redirect of a certain domain - apache

I want to redirect all request of one particular domain to another, except for one page request. I hope my attempt explains what I try to do:
RewriteEngine on
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
Rewritecond %{HTTP_HOST} !^host\.mysite\.com
RewriteCond %{REQUEST_URI} !^/link/
RewriteCond %{REQUEST_URI} !^dokument_by_link\.php
RewriteRule (.*) https://www.anothersite.de/$1 [R=302,L]
#This should not apply to host.mysite.com, but I think this is already accomplished by the L-flag above
RewriteRule ^test/?$ test.php [L,NC]
host.mysite.com/link/ should be redirected to host.mysite.com/document_by_link.php
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L]
All other requests should be redirected to https://www.anothersite.de
Thank you very much!

This should do the trick :
RewriteEngine on
#rewrite /link to /document_by_link.php
RewriteRule ^link/?$ /document_by_link.php [L]
#redirect all other URLs to https://www.anothersite.de
RewriteRule !dokument_by_link\.php$ https://www.anothersite.de%{REQUEST_URI} [L,R]

This should work for you:
RewriteEngine On
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteCond %{THE_REQUEST} !\s/link/ [NC]
RewriteRule ^ https://www.anothersite.de%{REQUEST_URI} [R=302,L,NE]
Rewritecond %{HTTP_HOST} ^host\.mysite\.com$ [NC]
RewriteRule ^link/([^/]+)/([^/]+)/([^/]+)$ dokument_by_link.php?$1=1&document_type=$2&value=$3 [NC,L,QSA]
RewriteRule ^test/?$ test.php [L,NC]
Using THE_REQUEST instead of REQUEST_URI here as REQUEST_URI may change to a rewritten URI whereas THE_REQUEST remains same for the scope of a web request.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Related

htaccess rewrite if url doesnt have a query string

I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.
With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

Too many redirects with mod_rewrite (at most one redirect?)

I want there to be at most one redirect using my htaccess file. Is that possible?
my htaccess file:
RewriteEngine On
#remove index/index.html
RewriteRule index\.html|index https://example.com/ [R=301,L]
#redirect to non-wwww and https site
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]
#remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
If I enter for example http://example.com/foo.html or http://example.com/index then I have two redirects.
If I enter for example http://example.com/ or https://example.com/foo.html then I have only one redirect.
What I want:
I want at most one redirect (see last two examples).
If I navigate to the home page, then I always want the index/index.html to be removed
All http calls should be redirected to https calls
All URL which are called with www, should be called without www
If I navigate to the subpage (https://example.com/foo.html), then only the html should be removed respectively the user can call the page without .html (https://example.com/foo).
Is this possible? Currently sometimes two redirections happen.
Could you please try following, based on your shown samples. Please make sure you clear your browser cache before testing URLs.
RewriteEngine ON
##Apply https and remove www together for all requests here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [NE,L]
##Server files with backend .html files.
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.html [L]
##Deal with home page .html stuff here.
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ index/index.html [L]

How can I set the RewriteCond for the exact URL only?

I need to make a hidden redirect from sitename.dom to sitename.dom2 keeping the rest of string untouched.
For now I use:
RewriteCond %{HTTP_HOST} ^sitename.dom
RewriteRule ^(.*) http://sitename.dom2/$1 [P]
and it works perfectly. But. Due to multilanguage on my website the frontpage has the following path:
sitename.dom2/lang
thats why when user calls sitename.dom he is being redirected (hidden) to sitename.dom2/ and he's getting 404 page.
So, please advise how do I make a strict redirect for exact request only sitename.dom without any further?
I had tried
RewriteCond %{HTTP_HOST} ^sitename\.dom$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*) http://sitename.dom2\/lang [P]
with no luck.
BTW, inside the website language subpath doesn't affect at all. sitename.dom/lang/page works as good as sitename.dom/page
I had to add the single redirect before all other. And use dom2 instead of dom1 in this rule.
Here is the solution:
RewriteEngine On
#redirect front page only:
RewriteCond %{HTTP_HOST} ^sitename\.dom2$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sitename\.dom2$
RewriteRule ^/?$ "http\:\/\/sitename\.dom2/\lang" [L]
#redirect all other pages:
RewriteCond %{HTTP_HOST} ^sitename\.dom2
RewriteRule ^(.*) http://sitename\.dom1\/$1 [P]
Simply add /lang in your first RewriteRule directive:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sitename.dom$
RewriteRule ^ http://sitename.dom2/lang%{REQUEST_URI} [P]

htaccess problems with redirect for no www

I've been trying everything to manage a redirect from www.domain.com to domain.com,
but nothing seems to work for me. I always get a redirect loop - and I've tried various things I found here or on Google.
So here is my .htaccess, maybe someone could help me figure out what I can do to redirect correctly or if there is something wrong in here.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# Redirect all to .php
# Example: example.com/hello -> example.com/hello.php
RewriteRule ^(.*)$ $1.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
Thank you so much!
I've already spent so much time trying to figure this out.
You have a rule that always matches, which is responsible for the infinite redirection. I've updated your ruleset below to fix that problem and perform the redirection you mentioned at the top of the answer. Let me know if this does what you expect.
RewriteEngine On
# Redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^.*$ http://example.com/$0 [R=301,L]
# This performs an external redirection? Is that what you want?
# Don't do the rewrite if we're already pointing at a file, otherwise we'll
# just redirect over and over because .* matches what we redirect to, too
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.+$ $0.php [L,R=301]
# show example.com/index.php always as example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
The answer is Apache documentation, the documentation tell how to force usage of www. You just have to reverse the example.
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://example.com/$1 [L,R]