I have an .htaccess file:
SetEnv PHP_VER 5_TEST
RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/pay|ok|ko|retour/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(blog|www|flashgames).sample.com$
RewriteRule ^(.*) http://www.sample.com/$1 [QSA,L,R=301]
ErrorDocument 404 https://www.sample.com/index.php
I would like to add that rule at the begib
when url is : http://www.sample.com/test.php then Returns (DO NOTHING), just bypass all rules.
Any idea ?
You can use a dash - as the substitution for RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
With an L|last flag, it will skip all following rules. This means, when you insert a rule at the beginning
RewriteRule ^test\.php$ - [L]
it will just process test.php, as if there were no rules at all.
When you insert this rule somewhere in the middle, Apache will process all rules up to this point and then exit the rule chain.
Related
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
I am trying to learn how to use RewriteRule.
I have 2 'pages' (wordpress)-
domain.com/webinars/ (shows list of all webinars)
and
domain.com/webinar/ (shows specific webinar details)
I have trying to set the following conditions -
(1) domain.com/webinars/{Year}/{Month}/ will load /webinar/?year={Year}&month={Month}
(2) domain.com/webinar/ (with no /{Year}/{Month}) will load /webinars/
(3) domain.com/webinar/?year={Year}&month={Month} will redirect to /webinars/{Year}/{Month}/ and then apply condition #1
This is my attempted code
RewriteEngine On
RewriteRule ^/webinars/([0-9]+)/([A-Za-z0-9]+)/$ /webinar/?year=$1&month=$2 [NC]
RewriteRule ^/webinar/$ /webinars/
RewriteRule ^/webinar/year=(\d+)$month=([\w-]+)$ /webinars/%1/%2? [R=301,L]
Condition #1 results in a 404 page not found
Condition #2 shows /webinar/ and not /webinars/
Condition #3 stays on domain.com/webinar/?year={Year}&month={Month} and does not redirect
What am I doing wrong? Only other code in my htaccess file is the default wordpress block.
Try the following :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^webinars/([^/]+)/([^/]+) /webinar/?year=$1&month=$2 [NC]
The rest could be done the same way .
Try with below rules,
RewriteCond %{REQUEST_URI} ^/webinar$
RewriteCond %{QUERY_STRING} ^year=([^/]+)&month=([^/]+)
RewriteRule ^ http://example.com/webinars/%1/%2/? [R=302]
RewriteCond %{REQUEST_URI} ^/webinars/([^/]+)/([^/]+)/$
RewriteRule ^ webinar/?year=%1&month=%2 [L,END]
RewriteCond %{REQUEST_URI} ^/webinar/$
RewriteRule ^ webinars/ [L]
You can use this
RewriteEngine on
# if /webinar/ is requested skip the rules and serve /webinar/ directory
RewriteRule ^webinar/?$ - [L]
#redirect /webinar/?year={year}&month={month}
#To /webinar/year/month
RewriteCond %{THE_REQUEST} /webinar/\?year=([^\s&]+)&month=([^\s&]+) [NC]
RewriteRule ^.+$ /webinar/%1/%2? [L,R]
# rewrite new url to the old one
RewriteRule ^webinar/([^/]+)/([^/]+)/?$ /webinar/?year=$1&month=$2 [L,NC]
We distribute different versions of a software product through a single download link. The delivery is based on the referer in conjunction with a default value, which works fine. In addition the user should be redirected to a 404-page, in case the wrong filename was used.
At the moment the .htaccess-file looks like this:
# stop directory listing
Options -Indexes
# turn rewrite engine on
RewriteEngine On
# force 404 if file name is missing or wrong
RewriteCond %{REQUEST_URI} !^(download_mac\.zip|download_pc\.zip)$
RewriteRule (.*) 404/index.html [L]
# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ domain_ab/$1 [L]
# last rule if no referer matches
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ default/$1 [L]
So I have one issue and one additional question with this file:
The first rule, to force 404, is very greedy and gets the error page every time, no matter what URL is called. I also tried single statements like RewriteCond %{REQUEST_URI} !^download_mac\.zip$ without any effect. How can I fix this?
How can I get rid of the filenames in any other rule? I tried things like RewriteRule ^(.*)$ default/$1 [L] but it gives me a hard time and an 500 Internal Server Error.
You can avoid repeating your filenames by using an Env variable like this:
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ - [E=ALLOWED:$1,NC]
RewriteCond %{ENV:ALLOWED} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /404/index.html [L]
RewriteCond %{ENV:ALLOWED} !^$
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^ /domain_ab/%{ENV:ALLOWED} [L]
RewriteCond %{ENV:ALLOWED} !^$
RewriteRule ^ /default/%{ENV:ALLOWED} [L]
You can just move the rewrite rule to the end. The other rules handle the valid cases and if none of them matches the last rule applies
# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-[ab]\.com
RewriteRule ^download_(mac|pc)\.zip$ domain_ab/$0 [L]
# last rule if no referer matches
RewriteRule ^download_(mac|pc)\.zip$ default/$0 [L]
# force 404 if file name is missing or wrong
RewriteRule ^ 404/index.html [L]
I need to have :
http://www.example.com/v1/my-project/ redirected to http://example.com/my-project/
so :
(1) remove the www from the http_host
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
(2) remove the 'v1/' part of the request_uri
RewriteCond %{REQUEST_URI} ^/v1/(.*)$ [NC]
RewriteRule . %1 [R=301,L]
(3) I also want to redirect all 404 to the homepage.
ErrorDocument 404 /
(4) Finally, all my documents actually reside in a "v2/" folder which hosts the current active website, but i don't want "v2" in the url, just "/"
RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule ^(.*)$ /v2/$1 [NC,L]
So, here are my rules. My question is: i don't manage (2): it gets redirected to / (because of rule (3) i guess. I think the order of my rules must be faulty but i can't seem to get it right. Can you help ?
"Rule 3" isn't a rule at all, and its order relative to your RewriteRules doesn't matter. Rule 2 is failing for some other reason. I'm not sure whether it will address your problem, but I would simplify your rules somewhat by writing them like this:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteRule ^v1/(.*) /$1 [R=301,L,NC]
RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule (.*) /v2/$1 [NC,L]
You should first write any rule that is causing an external redirect (R flag) and then the other rules. Otherwise an already rewritten URL can be used for an external redirect though it was just intended for an internal redirect.
So I won’t change the order you have right now.
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