Redirect in .htaccess makes me cracy - .htaccess

Following ws call https://training-deluxe.de/nlpdocs/podcast/feed/
should be redirected to podcast hoster podigee
.htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.training-deluxe.de/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
#Whats wrong with the next line
RewriteRule /nlpdocs/podcast/feed/ https://coachingundwissenschaft.podigee.io/feed/mp3 [R=301,L]
RewriteRule /coaching_ausbildung/gesundheitscoach_somatic_release_achtsamkeit.html https://rubin-institut.de/health-practitioner-und-gesundheitscoach/ [L,R=301]
RewriteRule ^(nlpdocs/.*)$ https://www.rubin-institut.de/$1 [R=301,L]
RewriteRule ^(.*)$ https://rubin-institut.de/$1 [L,R=301]
Redirect goes to rubin-institut/nlpdocs/podcast...
I cant get the clue

There's a couple of issues...
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.training-deluxe.de/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
You need to remove that 2nd/last RewriteCond directive above as that will break the rule that follows. RewriteCond directives are conditions that apply to the first RewriteRule directive that follows.
#Whats wrong with the next line
RewriteRule /nlpdocs/podcast/feed/ https://coachingundwissenschaft.podigee.io/feed/mp3 [R=301,L]
The first argument to the RewriteRule directive takes a regular expression (regex) - as you've used in later rules. It is not a simple URL-path. And, importantly, in .htaccess the URL-path matched by the RewriteRule pattern does not start with a slash. (You have omitted the slash prefix in the later rule that is evidentally "working".)
It should be like this instead:
RewriteRule ^nlpdocs/podcast/feed/$ https://coachingundwissenschaft.podigee.io/feed/mp3 [R=301,L]
You will need to clear your browser cache before testing since the erroneous 301 (permanent) redirect will have been cached by the browser. Test first with 302 (temporary) redirects to avoid caching issues.
You will also need to check the rule that follows, as that looks like it would have the same problem.
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

Continuous redirections in .htacess

How to get rid of the error that causes continuous redirection? I would like these rules to work in both directions.
RewriteEngine On
RewriteRule ^search/([a-z0-9-]+)/$ search.php?name=$1
RewriteCond %{QUERY_STRING} ^name=(.*)$
RewriteRule ^search\.php$ https://example.com/search/%1/? [L,R=301]
there's a redirection loop
you may use an environnement variable to check if your url is in a Redirection context
# redirect pretty url to real php
RewriteRule ^search/([a-z0-9-]+)/$ search.php?name=$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=(.*)$
# force pretty url with 301 unless in a rewrite
RewriteRule ^search\.php$ https://example.com/search/%1/? [L,R=301]

htaccess rewrite needs one exception

I'm having trouble defining an exception for a single URL on a site that needs to all be https except one subdirectory.
I currently have:
## Redirect URLs to https://
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
But I need to define an exception around www.example.com/forum so that it's always http://www.example.com/forum, without the https:// protocol.
How can I do this? I've tried all kinds of ! operators on RewriteCond, but the %SERVER_PORT seems to take precedence. Is there another RewriteCond I should consider?
Replace last line :
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
With this :
RewriteRule !^/?forum https://www.example.com%{REQUEST_URI} [R,L]
If it is Ok , replace [R,L] with [R=301,L] to be permanent redirection
Update
As per your comment that you want to force any https request contains forum into http you could add these rules along with rules above :
RewriteCond %{SERVER_PORT} !80
RewriteRule ^/?forum http://www.example.com%{REQUEST_URI} [R,L]

.htaccess redirect loop when Redirect and RewriteRule together

I'm attempting the following in an HTACCESS file:
I want to 301 redirect this --> http://www.domain.com/somepage.php?page=foo
to this --> http://www.domain.com/my-pretty-url/
This works fine when I alter internal links on the site to read how I want and I DO NOT use the R=301 flag:
RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]
BUT... the hitch here is I also want to 301 Redirect any external requests to the server, which when I handle that it puts me in a redirect loop.
RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]
RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]
The RewriteCond rules don't work by themselves, only the single RewriteRule at the bottom works by itself for internal rewrites, but it doesn't handle outside requests.
Obviously, if I have both together, it's creating a loop. How do I get around this??
Thanks!
Try the following to prevent the looping
#prevent internal redirects, and prevent loop
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]
RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]

Is my redirect not removing final slash?

I have a site that I don't think my htaccess is implementing 301 redirects properly, I think my problem may be at the application level but I would appreciate it if someone can confirm the following works as I expect it to.
Example redirect: http://www.siteic.com/a/b/c/ should go to http://www.siteic.com/a/b/c - however the below doesn't do that. NOTICE the trailing slash is removed.
Options +FollowSymLinks
RewriteEngine on
ErrorDocument 404 /
RewriteCond %{HTTP_HOST} ^siteic\.com
RewriteRule ^(.*)$ http://www.siteic.com/$1 [R=permanent,L]
RewriteCond {REQUEST_URI} ^/first/$
RewriteRule ^(.*)$ http://www.siteic.com [R=permanent,L]
RewriteCond {REQUEST_URI} ^/first$
RewriteRule ^(.*)$ http://www.siteic.com [R=permanent,L]
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ otherst.php?page=$1
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ otherst.php?page=$1&page2=$2
I would suspect you first rule to be always true, therefore the subsequent rules are never used.
You should use that instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]
Your second rule makes sure all uri with the string '/first/' redirect to the homepage.
Your third rule does exactly the same with the string '/first' so in fact you could remove your second rule, or your third one depending on your intended behaviour.
Your fourth rule will never match because uri with "first/" will have been redirected to homepage.
Your fifth rule should come before your fourth rule because rule 4 will be more often true than rule 5.
As to your request: how to remove the trailing slash, i would do this:
- first make sure the path points to a non-existent directory
- if true, remove the slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
So in the end i would do this:
RewriteEngine on
# force WWW in the url
RewriteCond %{HTTP_HOST} !^www.siteic.com$
RewriteRule ^(.*)$ http://www.siteic.com/$1 [R=301]
# remove trailing slash if the url points to a non-existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
# redirects all url with /first to otherst.php
## with 2 GET vars
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$
otherst.php?page=$1&page2=$2
## with 1 GET var
RewriteRule ^first/([a-z,\_%,A-Z,\_%-,0-9,\-]*)$ otherst.php?page=$1

Weird .htaccess url rewrite discrepancy

So http://myopicvoid.org/ when loaded in Firefox or Chrome, automatically redirects to http://myopicvoid.org/main as well it should, but not in IE8. What exactly would cause this? My .htaccess is as follows:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /main [r=301,L]
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} !^/script/.*
RewriteCond %{REQUEST_URI} !^/style/.*
RewriteRule ^(.*)$ /script/$1.py [L]
There exists a main.py in /script, but I'm met with Error 404 Not Found (script not found or unable to stat: script/.py) in IE. Help?
I would recommend changing
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /main [r=301,L]
To:
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteRule ^/?$ /main [r=301,L]
So, what I've done is removed the condition, but made the rule only match either nothing, or just a single fore-slash. The condition wasn't really necessary; it's important to note that mod_rewrite processes rules first, then checks to see if they meet their conditions, so this should be a tiny bit more efficient.
This will be a little more forgiving if the request does not include the trailing slash.
Off the top of my head - maybe the URL http://myopicvoid.org/ has the trailing slash removed when you make the request from certain browers? This would prevent the "/" from matching the first RewriteRule.

Resources