Apache rewrite for 404: getting the requested url - apache

On my error page that I redirect to for any 404s, I'd like to record the url that the user tried to get to.
I've tried this but it doesn't work:
ErrorDocument 404 /error/?url=%{HTTP_REFERRER}
Can anyone tell me how to do it?

Try it with %{REQUEST_URI}. I'm not certain this will work in ErrorDocument since I've never tested it, but it's worth trying.
ErrorDocument 404 /error/?url=%{REQUEST_URI}

There isn't a direct way. Nor a perfect one. But there are few workarounds with PHP.
For example, I currently use a function to create the links of each page. So I would just need to add file_exists() to the main function (few lines in a single function).
This is the function I would use to create urls:
function url ($Value)
{
// Do some stuff with the url
// [Not showed]
if (!file_exists("/internal/path/".$Value))
{
// Call a function to store the error in a database
error ("404", $Value);
// One way of handling it. Replace '/' for ' ' and search that string.
// Example: "path/to/page" would become "path to page".
$Value=str_replace("/","%20",$Value);
return "http://www.example.com/search=".$Value;
}
else
{
// If the page exists, create the normal link.
return $FullUrl;
}
}
This is my regular way of creating an urls:
<?php url('path/to/page'); ?>
I just thought about this method. It's great as it allows you to find missing pages even IF the user doesn't click on the links. Thank you for making me think about it and now I'll use it in my page (:
Another 'simpler' method (in case you do not wrap links) is that you store last couple of pages visited in $_SESSION['lastpage']; and $_SESSION['lastlastpage'];, if 404 is found then store the corresponding page from which the user tried to access the broken page. It's not a perfect solution since you have to manually find the broken link in the previous page, but at least it gives you some idea of where it is.
Disadvantage: As you can see, both solutions ONLY work with internal broken links.

It would seem there isn't a way.

Related

How to find broken links in a website using Cypress?

How can I find all broken links in a website using Cypress if there are any? Are there any plugins to use?
If not, how should I write my code that I can see whether the link returns a 200 status (or a successful link)?
Thank you!
Maybe this?
cy.request('yourlink')
.should((response) => {
expect(response.status).to.eq(200)
})
Try this...
for the first a selection a[href*="yourdomain.com"] the reason I didn't include "www" is because for my project we link to subdomains as well so I needed to include these.
for the second half, :not([href=""], again for my project we had links like this for some reason so I had to make sure to NOT include it.
$el.prop("href") fetches the href value from the DOM element.
I'm using cy.request() to send a HTTP request to the URL and make sure that it returns 200 thus proving that the page exists.
Here's the code:
cy.get('a[href*="yourdomain.com"]:not([href=""])').each(($el) => {
cy.request($el.prop("href")).as("link");
});
cy.get("#link").should((response) => {
expect(response.status).to.eq(200);
});
You can use this for any page. You can have your pages listed out in your cypress.json file or you can do it programatically as a regression test.
Here's the simplest way to do it:
cy.request("yourLink")
Yes, no need to add any assertions. because by default there's an assertion that the response is not 404. so this line is more than enough.
If you want to go the extra mile and test all the links in the page, here's what to do:
cy.get('a').each(link => {
cy.request(link.prop('href'))
})
This should make a request to all the available links with a tag in the page.
cy.visit(link)
cy.get('a').each(page => {
cy.request(page.prop('href'))
})

Rest API from PHP

I have a PayPal button which upon checkout completion redirects the user back to the homepage but in the background opens another php page which does a couple of things including updating some databases etc. However in it it I'd also like to perform a rest API request.
http://your-server/rest/createUser?username=testuser&password=testpassword
Here is the URL I need to fire and I'm not too bothered about reading the contents of this to be honest, it respond an XML file saying status OK or error messages but I don't need this... All I want to do is access the URL. This works from a browser, but when I try to use
$apicall = file_get_contents("http://your-server/rest/createUser?username=testuser&password=testpassword");
It doesn't work... Any thought ?
For file_get_contents() to work correctly any special characters have to be encoded.
$url = 'http://your-server....';
$encodedUrl = urlencode($url);
$apicall = file_get_contents($encodedUrl);
The other thing to check is that allow_url_fopen php.ini setting is set to true. (It is by default).

Stencil redirect after login

Is there a good way to redirect to a specific page using Stencil right after logging in? It defaults to the my account, but want to send to main shop page. I know login.php?from= works, just not sure how to enforce that and looking for a better way.
I'd recommend a javascript redirect based on the referring URL, though it isn't ideal. There isn't a store setting to accomplish this.
if (document.referrer !== "http://www.stackoverflow.com") {
window.location.href = "http://www.google.com";
}
There are alternative scripts to accomplishing this that can be found if you look for "redirect by referring url" or "redirect based on previous page".

How to make a redirect in velocity template?

How to redirect to http://google.com in the code .vm file? (I mean within
#if <redirect to Google here> #else ... #end statement)
Doing
setRequestURI('http://google.com')
or similar doesn't work and I'm not sure if it is possible at all.
Thank you.
Can anybody explain please?
IMHO a template is the wrong place to implement logic like this. You should determine upfront (in whatever you do - you don't mention your environment other than velocity), before you determine that the velocity template in question should render the output.
Once you're in the template, you can't assume that you're within a web browser (it might render an email message) or that you have access to the response headers (the template might be rendered as part of a page when the response has already been "committed", e.g. the headers are already sent to the browser.
Do yourself a favor and move the logic further up the chain, out of your velocity template. If you need a quick fix: render Javascript that triggers the redirect. But pay back this technical debt sooner rather than later.

Yii how to redirect to particular URL

I have used url beautifier to remove index.php?r= from URL. it works fine.
now i want user to redirect to URL if user hits particular url.
suppose
http://localhost/xm/xmds.php?wsdl
To
http://localhost/xm_demo_menu/WebService/service
how to achieve this. i search lot on google but not found material relevant to my problem
any suggestions ?
I do not see where the problem should be. See documentation
In you controller, simply call:
$this->redirect("http://localhost/xm_demo_menu/WebService/service");