I want to rearrange my web site, and move all customer pages into a directory (/customerPages), while keeping the same URL (URL to access the page and the URL that is showed in the browser). I'm using Apache and PHP (CakePHP).
I've tried rewiring my 404 error page in the following way:
// Redirect customers to customers landing pages under /customerPages
if (strpos($message,'customerPages') == false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
$homepage = file_get_contents($url);
echo $homepage;
}
But this solution breaks all images written using relative paths.
Later I've tried using redirection instead:
if (strpos($message,'customerPages') == false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
header("Location: ".$url);
}
But than the URL changes. I've tries fiddling with RewriteRule with no luck.
How can I achieve this using the first,second or any other method?
Another way, just basic idea:
1. Put in your /oldpath/.htaccess file (we handle file not found 404 error):
ErrorDocument 404 /oldpath/redirect.php
2. /oldpath/redirect.php file (our handler) - make redirect to new path only if file exists in newer location:
$url = parse_url($_SERVER['REQUEST_URI']);
$filename = basename($url['path']);
if(file_exists('/newpath/'.$filename)) {
header('Location: /newpath/'.$filename);
}else{
header('Location: /your_real_404_handler');
}
You need to redirect image requests from newer location (/customerPages) to the old path.
You can use mod_rewrite apache module to redirect such requests back:
RewriteEngine on
RewriteRule ^/customerPages/(.*\.jpg|.*\.gif|.*\.png) /oldpath/$1 [R]
Related
I moved approx 100 articles from my old website to a new one. I want to create a redirect in the old site's header, so if the old URL of an article would be visited, the visitor should be redirected to the new URL of that article.
However, just replacing the domain won't do the trick, as I changed the permalink of the articles. So I would need some "database" (with arrays?) which would decide if the actual URL has a redirect in the database, like:
// "the old permalink" = "the new permalink"
$urlpermalink["article-cars"] = "http://NewWebsite.com/new-cars-article";
$urlpermalink["an-article-dogs"] = "http://NewWebsite.com/new-dogs-text";
$urlpermalink["old-text-trees"] = "http://NewWebsite.com/new-blogcontent-about-trees";
So for example if a visitor visits "http://OldWebsite.com/article-cars", he should be redirected to "http://NewWebsite.com/new-cars-article", because that's how the database/array says.
And so I could something like this:
$visitingurl = $_SERVER[REQUEST_URI]; // Getting the URL the visitor is on now
foreach( $urlpermalink as $value ) { // For every entry in the database/array...
if (strpos($visitingurl, $urlpermalink) !== false) { // check if the visitingurl contains that (like "article-cars")
// The visitor is indeed on an old URL which is in the database/array, so let's redirect him to the new URL
header("HTTP/1.1 301 Moved Permanently");
header(url . $_SERVER['QUERY_STRING']);
exit();
}
}
Of course this code is totally wrong, but I have almost no idea about PHP, so could you please help me solving this? (a JavaScript-solution is fine too, or htaccess, or whatever :) )
Thank you very much!
I think I solved this now with htaccess (I have no idea about htaccess either, just googled even more).
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectMatch 301 ^/article-cars/ http://NewWebsite.com/new-cars-article
RedirectMatch 301 ^/an-article-dogs/ http://NewWebsite.com/new-dogs-text
RedirectMatch 301 ^/old-text-trees/ http://NewWebsite.com/new-blogcontent-about-trees
</IfModule>
Is this okay this way?
I have a domain which I want every attempted url after https://www.example.com/someFolder/ to not give an error but instead give a php page.
For example, I do not have a somefolder/abc.php file but going there will run a process.php file instead and display it there.
I have attempted to modify the 404 page as the process.php but that also modifies the example.com/ error page which I do not want.
It would be great if I do not need to modify/add a file at the root directory
PS. adding a .htaccess to the somefolder folder does work somewhat but then the url shows somefolder/404.php and not somefolder/abc.php
Modify your 404 page as you did putting php script there but check in php if the url that was requested was directory or not. Depending on that make an appropriate action
<?php
$url = $_SERVER[REQUEST_URI];
// check if requested url was ending with slash
$pattern = '#\/$#';
$result = preg_match($pattern, $url);
if ($result === 1) {
//request was to directory suffixed with slash
//do your php code for custom 404
die; //end the page generation
}
?>
request was not to directory ending with slash
put html of regular 404 page here or leave it blank
I have learned that I could turn somefolder into somefolder.php and use the $_SERVER['PATH_INFO'] to make all pages exist.
This is something that can only be done, in the general case, by using the .htaccess file, and redirecting every request like this...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,NC,L]
After that, you can use $_SERVER, $_SESSION, and other global variables in index.php to decide how to handle a 404 error (i.e., you can implement here how to define what a 404 is, etc.).
For instance, $_SERVER['PATH_INFO'] will be able to tell you what URL was requested, and you can use a combination of file_exists() and other calls, to determine if you want to display a full 404, search results, a redirect, or simply to display the results of another script.
This works for example.com/somefolder/, example.com/some/other/folder/, and example.com/a/b/c/d/e/f/g/, etc..
So I've moved my website from site.com to sub.site.com. I have also moved the booking system.
Is it possible for me to have PHP code on index.php on site.com to check if the user wanted site.com/unbook.php to automatically redirect them to sub.site.com/unbook.php
The optimal thing would be some kind of "redirect everything if there is anything in the url other than site.com"
Something like
$urlafter = explode("site.com", $url);
if (strlen ($urlafter[1]) > 0 ) {
header("Location: sub.site.com".$urlafter[1]);
exit();
}
Or would it be better using something like $_SERVER[REQUEST_URI] or even .htaccess
-- EDIT --
Non duplicate because they only show .htaccess answers, I'd prefer PHP
Using Htacces is better for the scenerio.
In the .htaccess file at the site.com you can set the following rule to redirect:
Redirect 301 / http://sub.site.com/
301 is for permanent redirect.
If the redirect is temporary, you can use 302 instead.
In Php you can use the following code to replace the domain:
$uri = $_SERVER['REQUEST_URI'];
$old_domain = 'yoursite.com';
$new_domain = 'sub.yoursite.com';
$old_domain_pos = strpos($url, $old_domain);
$redirect_url = substr($url, 0, $old_domain_pos).$new_domain.substr($url, ($old_domain_pos+strlen($old_domain)));
The above code replaces the domain only once, leaving everything else as it is.
If your url is like the following, where the domain may repeat in the querystring, which themselves need to be replaced.
http://www.yoursite.com/get/?assset=www.yoursite.com/imgs/pic01.png
then you can consider string_replace method.
$redirect_url = str_replace($old_domain, $new_domain, $url);
I have a dilemma here...
At the moment I'm launing a website and there are a lot of old url that needs to be redirected to it's new url without .stm extension. And the new site is running Durpal.
Ex.
http://www.foo.com/foo/bar.stm --> http://www.foo.com/foo/bar
So I went looking more into nginx.conf setting to rewrite the url before it even hit setting.php that is being used by drupal. Turns out that my host doesn't let me touch the conf file on the root folder. but my only solution is add this to the conf file in root
server { location / { rewrite ^(.*)\.stm$ $1 permanent; } }
At this point, I'm running out of options but to redirect every pages in Drupal manually ( that is roughly about 650 pages )
I've also tried every other modules, but non offers extension strip redirect.
My other option is using 404 landing page to run php and redirect any url with .stm and redirect with scrubbed url without .stm and stays in 404 if it's not a .stm url.
This is my best bet, URL rewriting with PHP
But it's over complicated just for a extension strip and redirect?
You should be able to check if the requested URI contains ".stm", and if so then redirect to the exact same URI with the ".stm" ripped. Put something like this in your settings.php:
if (strrpos(request_uri(), '.stm') !== FALSE) {
header("Location: " . str_replace('.stm', '', request_uri()));
exit;
}
I would like to quickly set up a RESTful site using PHP without learning a PHP Framework. I would like to use a single .htaccess-file in Apache or a single rule using Nginx, so I easyli can change web server without changing my code.
So I want to direct all requests to a single PHP-file, and that file takes care of the RESTful-handling and call the right PHP-file.
In example:
The user request http://mysite.com/test
The server sends all requests to rest.php
The rest.php call test.php (maybe with a querystring).
If this can be done, is there a free PHP-script that works like my rest.php? or how can I do this PHP-script?
Using Apache Mod Rewrite:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^test$ rest.php [nc]
Yes, make a 404 Redirect to a page called rest.php. Use $url = $_SERVER['REQUEST_URI']; to examine the url. In rest.php you can redirect to wherever you want.
This only requires one rule (404 in your .htaccess file).
You have to be careful and make sure that if the page requested (e.g. mysite.com/test1 doesn't have a test1.php) errors out with a 404 you don't get yourself caught in a loop.
Modified slightly from the way that Drupal does it. This redirects everything to rest.php and puts the original requested URL into $_GET['q'] for you do take the appropriate action on. You can put this in the apache config, or in your .htaccess. Make sure mod_rewrite is enabled.
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ rest.php?q=$1 [L,QSA]
If all you then want to do is include the requested file, you can do something like this
<?php
if (!empty($_GET['q'])) {
$source = $_GET['q'] . '.php';
if (is_file($source)) {
include $source;
} else {
echo "Source missing.";
}
}
?>
You really don't want to do that, however; if someone were to request '/../../../etc/passwd', for example, you might end up serving up something you don't want to.
Something more like this is safer, I suppose.
<?php
if (!empty($_GET['q'])) {
$request = $_GET['q'];
switch ($request) {
case 'test1':
include 'test1.php';
break;
default:
echo 'Unrecognised request.';
break;
}
}
?>