Redirecting Entire Site with Zeus - zeus

I am trying to redirect an entire site that is on a Zeus server. I have only managed to successfully redirect the homepage. Other pages are coming up with "Page not found". Here's the code I used:
match URL into $ with ^\/$
if matched
set OUT:Location = http://www.mentalhealthreform.ie/
set OUT:Content-Type = text/html
set RESPONSE = 301
set BODY = Moved
goto END
endif
Any help would be much appreciated!

you might want to try:
RULE_0_START:
match IN:Host into $ with currentsite\.co\.uk$
if matched then
match URL into $ with ^/(.*)$
set OUT:Location = http://www.newsite.com/$1
set OUT:Content-Type = text/html
set RESPONSE = 301
set BODY = Moved
goto END
endif
RULE_0_END:
This should match the page request on the current site and append to the new. Our servers are now on Apache, but I do remember the difficulties in doing things with Zeus.

Related

How to set status code for certain URLs with IF in htaccess

I have URLs like https://example.com/page/1234/ and https://example.com/page/9876/. For these URLs i want set certain status code via htaccess (Apache 2.4).
I try to do it with
<If "%{REQUEST_URI} =~ (1234|9876)\/$">
Header set Status "HTTP/1.1 410 Gone"
</If>
but i seem to have an error in the code, because i don't see the new status code as response in developer tools. How should it be done on the correct way?
PS: i can't use 'rewriteRule' - this kind of setting 410 triggers ErrorDocument, what i don't want. For these two URLs i only want to set the status code. For other URLs, which get 410 on the native way, ErrorDocument shold be triggered.
You may use this block with <If> expression that uses default ErrorDocument 410 for a specific URL pattern:
RewriteEngine On
<If "%{REQUEST_URI} =~ m#/(1234|9876)/?$#">
# only for above URL disable ErrorDocument
ErrorDocument 410 default
# set status=410
RewriteRule . - [L,G]
</If>
Are you wanting the "normal page response" (as generated by your application), but with a 410 HTTP status?
100% correct: usual page, but with response status 410 Gone
Triggering the 410 response in Apache will always serve the appropriate Apache ErrorDocument. What you could do is set the 410 ErrorDocument itself to the same URL and trigger the 410 response in the usual way. However, we need to be careful not to create a "rewrite-loop".
For example:
<If "%{REQUEST_URI} =~ m#^/page/(1234|9876)/$# && %{ENV:REDIRECT_STATUS} == ''">
# Capture the URL-path after the slash prefix
SetEnvIf Request_URI "^/(.+)" REL_URL_PATH=$1
# Dynamic ErrorDocument to the same as the requested URL
# The slash prefix in the directive is necessary to be seen as a local URL-path
ErrorDocument 410 /%{reqenv:REL_URL_PATH}
# Trigger 410 Gone
RewriteRule ^ - [G]
</If>
This requires Apache 2.4.13+ due to the expression syntax in the ErrorDocument directive.
The check against the REDIRECT_STATUS in the <If> expression is necessary to avoid a rewrite loop (500 response) when serving the error document itself.
The alternative is to set the 410 response code in your application itself, which would be my preference.
Aside: Setting the Status HTTP response header, which appears to be what you are trying to do in the question simply sets a Status HTTP response header, it does not change the HTTP response status itself. (The Status response header is a non-standard header used by CGI scripts to indicate to the webserver what response code should be set.)
UPDATE: in my tests if i add to htaccess only the rule Header set Status "HTTP/1.1 410 Gone", without any condition, it works like i expect: all URLs from this directory get the header 410 Gone but are still available (ErrorDocument isn't triggered).
Maybe your server is configured differently and is perhaps behind a proxy that sets the HTTP response? But as mentioned above, that simply sets a Status HTTP response header, it doesn't change the HTTP response code on the request. It doesn't "work" on my test server. (If used at all, the Status header doesn't normally contain the protocol, it would simply be Header set Status "410 Gone".)
So, if this does work for you then you just need to "correct" the syntax in your Apache expression.
For example:
<If "%{REQUEST_URI} =~ m#/(1234|9876)/$#">
Header set Status "HTTP/1.1 410 Gone"
</If>
The above matches any URL that simply ends with /1234/ or /9876/. Or, to be more specific and match the entire URL use m#^/page/(1234|9876)/$#. This uses the alternative syntax for delimiting regex (ie. m#<regex>#), instead of using slashes (ie. /<regex>/), in order to avoid having to escape the slashes in the pattern.
Alternatively, you don't need the Apache expression, you could use SetEnvIf and set the header conditionally based on the environment variable.
For example:
SetEnvIf Request_URI "/(1234|9876)/$" GONE=1
Header set Status "HTTP/1.1 410 Gone" env=GONE
With your shown samples please try following. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^page/(1234|9876)/?$ - [NC,R=410,L]

Setting Apache mod_headers while we access directory any directory?

I am using apache 2.2. I have to set header for home page url only. e.g If url is https://www.example.com then I need to set header for this URL only. Could you please help me on this?
I have tried below things but no luck.
If condition used but not working for this version.
RewriteCond %{REQUEST_URI} /
Header set ABC "abc"
Here, Header is setting for all URLs in the application

How to unset a cookie using .htaccess

I'm using Apache; I put the following code in .htaccess to unset the Cookie header but it doesn't work:
<FilesMatch "\.(js|css|jpg|png|jpeg|gif|xml|json|txt|pdf|mov|avi|otf|woff|ico|swf)$">
RequestHeader unset Cookie
Header unset Cookie
Header unset Set-Cookie
</FilesMatch>
What is your solution?
Preventing the server from issuing a Set-Cookie response header for specific file types won't stop other file types setting a cookie for the domain. So the browser will still send the cookie and the benefits are lost.
Telling the server to remove a Cookie request header before passing it on to the next layer of request processing won't stop the browser from sending it in the first place. So the benefits are lost.
The article you reference in a comment says to serve your static files from a different domain.
Do that. Never write code that sets a cookie for that domain. That's all you need to do.
1 - Create a subdomain, such as static.yourwebsite.com, which is where you will deliver all your static files from
2 - Point your new subdomain to the /wp-content directory of your WordPress installation. For cPanel users, you will need to update the document root field from public_html/static to public_html/wp-content like the screenshot below.
3 - Edit your wp-config.php file to reflect the following
define("WP_CONTENT_URL", "http://static.yourwebsite.com");
define("COOKIE_DOMAIN", "www.yourwebsite.com");
4 - Run the following command in your SQL database, this will ensure all post URLs are directed to the new subdomain:
UPDATE wp_posts SET post_content = REPLACE(post_content,'www.yourwebsite.com/wp-content/','static.yourwebsite.com/')

Varnish 4 rewrite URL transparently

I am looking after a website that is currently running a pretty standard varnish/apache set up. The client needs to add a new domain that transparently serves from a path/query string in order to create a lightweight version of their site. For example:
The user visits mobile.example.com which points to the same server as example.com
Varnish rewrites the mobile.example.com request to example.com/mobile?theme=mobile
User receives the page served from example.com/mobile?theme=mobile by apache, but stays on mobile.example.com
We need to hit both a path and add the query string here, as well as maintain any path the user has entered, i.e: mobile.example.com/test should serve the content at example.com/mobile/test?theme=mobile
Any tips for doing this with Varnish 4? Is it possible?
Got it working!
if (req.http.host ~ "^mobile\.example\.com") {
set req.http.host = "example.com";
set req.url = regsub(req.url, "^/", "/mobile/");
set req.url = regsub(req.url, "$", "?theme=mobile");
}

How to get analytics stats for mod_rewrite redirects?

I have a website with about 200 apache mod_rewrite rules in httpd.conf, running on apache webserver in redhat.
Here's an example of one of the rules, most of them are short URLs that redirect to really long URLs:
RewriteRule ^grad2014/?$ /registration-and-records/graduation/live/index.html [R=301,L]
I've been asked to get some web analytics for these redirects.
"How many people used the URL mysite.com/grad2014?" - Well, I don't really know, because /grad2014 doesn't exist on the webserver, and google analytics are set up on the index.html page.
I don't seem to see any of the shortcuts in the access.log. Is there another way to see which URLs redirects are the most popular ? Is there a way to start logging this ?
One way you could do it:
Add the rewrite match as a query param on the target url:
RewriteRule ^grad2014/?$ /registration-and-records/graduation/live/index.html?rr=$0 [R=301,L]
Then in your GA code, capture that URL param and put it in a custom variable. I don't know if you know anything about custom variables, but here is an example of one way to set it:
// example function get query param. use your own if you already have one
function getParam(n){var n=n||'';var x=new RegExp("[\\?&]"+n.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)");var r=x.exec(window.location.href);return(r==null)?'':r[1]}
var rr = getParam('rr');
if (rr) {
_gaq.push(["_setCustomVar", 1, "Mod Rewrite Redirect URL", rr, 3]);
}
// your on-page trigger
_gaq.push(["_trackPageview"]);
NOTE: by default GA counts the page name as location.pathname+location.search so adding the rr param to the url is going to affect your pages report. The easiest way to fix this is to create a filter within GA to strip it from incoming page name (request URI), but if you need to, you can write some code to get location.pathname+location.search minus rr query param and populate that value as the 2nd element in your _trackPageview.
_gaq.push(["_trackPageview","custom page name here"]);
The access.log should give you what you want e.g.
cd /var/log/apache2 || cd /var/log/httpd
# Get a list of the 301 redirects issued.
grep 301 access?log
# Count the 301 redirects issued.
grep 301 access?log | wc -l
# Count a specific redirect
grep 301 access?log | grep grad2014 | wc -l