Redirect permanent, www redirect & url rewriting - .htaccess

I gave an old website a new CMS. Now i need a .htaccess with 3 parts, but it only works with 2 of them:
Redirect permanent
www redirect (this don't work)
url rewriting
Now i 'll explain something more.
Part 0 (only for the totality)
RewriteEngine On
Part 1 (Redirect permanent) i use to redirect the URLs of the old CMS to the content of the new one.
RewriteCond %{THE_REQUEST} !/de-old-content.html
Redirect permanent /de-old-content.html /old/content.html
Part 2 (www redirect) i use to avoid duplicate content.
RewriteCond %{HTTP_HOST} ^website.de [NC]
RewriteRule ^(.*)$ http://www.website.de/$1 [L,R=301]
Part 3 (url rewriting) to convert "old/content.html" into "index.php?adresse=old/content"
RewriteRule ^(.*)\.html$ index.php?adresse=$1 [l,qsa]
the [l,qsa] only allows to use $_GET.
Is it possible to combine this parts?
If i use Part 2 it shows the right content with
http://www.website.de/abc/def/ghi.html
but without www it redirect to
http://www.website.deabc/
I think there must be a misstake in this Part.
Thanks in advance.

Redirect is part of mod_alias, while RewriteCond and RewriteRule are part of mod_rewrite. As a consequence the RewriteCond of part 1 accually applies to the RewriteRule of part 2, which would make it not work properly.
Best to avoid using both rewriterule and redirect together.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.de [NC]
RewriteRule ^(.*)$ http://www.website.de/$1 [L,R=301]
RewriteRule ^de-old-content\.html$ /old/content.html [L,R=301]
RewriteRule ^(.*)\.html$ index.php?adresse=$1 [L,QSA]
PS. to keep your sanity, use 302's while testing. Once everything is working, change them to 301's. Since you already used 301's, also clear your browser cache, so all the old 301's are removed from the browser's cache.

Related

htaccess redirect url not working /?index.html to /

I have a client project on search engine marketing with a website https://www.iehsacademy.com/,
The site is 13 years old. before the client was using the URL structure like with HTML like this: https://www.example.com/?index.html (Home Page),
https://www.example.com/?cndc.html,
But last year they completely remake the website URL structure and make it to https://www.example.com/ (Home Page), https://www.exsample.com/about. https://www.example.com/contact.
Now, The old URLs and the new URLs are both working. The client wants and also for SEO purposes we want to block the old URL or redirect them to the new URL. I tried many rewrite conditions on htaccess
RewriteRule ^([a-z]+).html https://www.iehsacademy.com/ [QSA,L]
This one also
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com/?cndc.html$
RewriteRule (.*)$ http://www.iehsacademy.com/$1 [R=301,L]
Not Working anything. The client's developer is also confused. Can anyone help with this or where i am doing wrong?
NB: I took permission from the site owner to share the client's URLs and problems.
You can use the following redirection rule :
RewriteEngine On
#redirect /?index.html to /
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^index\.html$
RewriteRule ^$ https://example.com/? [L,R=301]
#redirect /?foobar to /foobar
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^$ https://example.com/%1? [L,R=301]
Make sure to clear your browser caches or use a different browser to test the redirection. And do not forget to replace the destination domain example.com with your domain name.

re-direct a domain to a sub-domain + make it https

this is kinda an odd one:
I need my site to do two things (one of which is already working):
if a user tried to access the domain via HTTP:// it is replaced with https:// - this is for SEO in google and to make the user feel more secure -
the site folder that is used to load the website needs to be the subdomain folder of the site
Oddly the second part of this is working and I figured out - however I'm not sure how to merge these two requests:
HTACCSESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trippy\.co\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.trippy\.co\.nz$
RewriteCond %{REQUEST_URI} !update.trippy.co.nz/
RewriteRule (.*) /update.trippy.co.nz/$1 [L]
But I'm not sure how to make the site display as
https://trippy.co.nz/
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} update\.trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://update.trippy.co.nz/$1 [R,L]
but then the web address displays as: https://update.trippy.co.nz
and I need to remain as https://trippy.co.nz/
Any help here would really great and I know its a odd situation to be in.
THanks,
Wally
...but then the web address displays as: https://update.trippy.co.nz
You would seem to be redirecting to the subdomain itself, not the subdomain's subdirectory, as you appear to be doing in the first rule. You may also be putting the directives in the wrong order - the external redirect needs to go first - otherwise you are going to expose the subdomain's subdirectory, which does not appear to be the intention.
Try the following instead:
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
# Rewrite all requests to the subdomain's subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteRule !^update\.trippy\.co\.nz/ /update.trippy.co.nz%{REQUEST_URI} [L]
No need for the extra condition in the 2nd rule block, as the check can be performed directly in the RewriteRule and use the REQUEST_URI server variable instead of the $1 backreference in the substitution string.
That that R by itself is a temporary (302) redirect. You may want to change that to R=301 (permanent) once you have confirmed this is working OK.

.htaccess rule to remove file extension conflicts with existing rewrite rules

I have working code in .htaccess that accomplishes the following.
redirect non-www to www (and https:)
redirect http to https (and www)
allow accessing URLs ending with /file1.htm etc at /file
However, as I discovered, the third rule was more of a hack that needed to be used in combination with removing all existing links to the .htm versions (which is outside your control). As a result, Google started crawling both versions and deciding which ones are canonical.
I want to modify the third rule to not just allowing access at /file, but rewriting the URL with a 301 message. There are several answers on Stackoverflow that have worked for others that are not working for me due to existing rules in the file, so I'm posting it as a new question.
How do I add a 301 redirect to all /file.htm links to /file without breaking the 3 existing rules?
RewriteEngine On
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
Redirect 301 /output.php /
I want to modify the third rule to not just allowing access at /file, but rewriting the URL with a 301 message.
Using a 301 there makes little sense – you want to keep this one an internal redirect.
You should add a new rule, that rewrites requests for /file.htm to /file externally, and have that one use a 301 status code:
#1) externally redirect "/file.htm" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.htm [NC]
RewriteRule ^ /%1 [NC,L,R=301]
#2) Internally map "/file" back to "/file.htm"
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*?)/?$ /$1.htm [NC,L]

How do I reference web root in .htaccess RewriteRule variable while using RewriteCond %{HTTPS} on

Here's what I am trying to do. I have a few scripts on a site I am rewriting the URLs to force them to use https://. What I then want to do, is rewrite urls when I navigate away from the HTTPS pages back to HTTP. Here's what I have now that is not working exactly the way I would like it to.
# For HTTPS pages:
RewriteCond %{HTTPS} off
RewriteRule ^(register/|cms/(.*))$ https://%{HTTP_HOST}/$1 [R=301,L]
# For non-HTTPS pages:
RewriteCond %{HTTPS} on
RewriteRule ^(about/|contact/)$ http://%{HTTP_HOST}/$1 [R=301,L]
The problem I am having is that while the pages like about/ and contact/ rewrite back to HTTP, I can't figure out how to reference the document root, so it unfortunately stays https://. I like using relative urls, so I would rather not go into my source and change everything to absolute if there is a simple htaccess solution.
My question: How do I properly reference the web root in my RewriteRule?
Also, is there a more efficient, catch-all way of doing what I am trying to accomplish that I just don't know about, because I haven't been able to find anyone else with this problem. I am not super-familiar with .htaccess. I learn just enough as I go to do various Rewrite operations, as I will never have a need for the full features, and I find the documentation cumbersome and difficult to follow.
Thanks!
for matching the root use
RewriteCond %{HTTPS} on
RewriteRule ^$ http://%{HTTP_HOST}/$1 [R=301,L]
or combine them with the other rule
RewriteRule ^(about/|contact/|)$ http://%{HTTP_HOST}/$1 [R=301,L]
As to the catch all expect /register and /cms (that's what you mean right?). I think this should do the trick.
# For HTTPS pages:
RewriteCond %{HTTPS} off
RewriteRule ^(register/|cms/(.*))$ https://%{HTTP_HOST}/$1 [R=301,L]
#prevent rules beneath this one in this htaccess file from being applied when url start with register of cms (doesn't do anything else)
RewriteRule ^(register/|cms/) - [L]
#don't force http for resources, to prevent partial encryption errors
RewriteRule ^(css/|images/) - [L]
# For non-HTTPS pages:
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Htaccess non-www and strip index.php in a nice way

I'm having some trouble with my .htaccess redirections.
I want a situation in which the (non-www)domain.tld is redirected to the www.domain.tld. And I want to rewrite the arguments to skip the index.php, making a request for /foo go to index.php/foo.
Initial situation
First I had these rules
RewriteCond %{HTTP_HOST} ^domain\.tld [NC]
RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\ /:-]*)$ index.php [L]
And this worked. Mostly. What didn't work was that in PHP $_SERVER['PATH_INFO'] stayed empty and I disliked the whitelisting of the characters.
Change for PATH_INFO and to accept more
So I changed the last line into this:
RewriteRule ^(.*)$ index.php/$1 [L]
This fixed the PATH_INFO and the limited characters. However, I recently noticed that this caused the non-www redirect to www. to fail miserably.. When going to the non-www domain Apache says
Moved Permanently
The document has moved here.
Where 'here' is linked to the same thing I typed (non-www domain.tld) and thus failing to serve the user.
Continuing the search..
I found a lot of Q&A here and elsewhere or the topic of non-www redirections, but all seem to fail in some way. For example:
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
This just didn't do that much. Nothing got redirected, although the website was served on the non-www.
Anyone knowing what I do wrong or having a solution for this mess? :)
(Preferably, I would like the non-www redirection to be global. So that I don't have to change the actual domain name every time.)
I guess you’re just missing the L flag to end the rewriting process:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And make sure to put this rule in front of those rules that just cause an internal rewrite.

Resources