I try to make a clean url with a simpel php code found here at stackoverflow. URL rewriting with PHP.
exampel.com/foo works
exampel.com/foo/ makes a 500 internal error
exampel.com/foo/bar also a 500.
I have tryed to figure why but i cant find the error. (i am not a php guru, but i belive i do understand some.)
my code to handel this fallback in index.php (fallback set in htacces)
$YdelseMenu = NULL;
function VisYdelser($element){
if(empty($element[0])) { // No path elements means home
$page2 = 'ydelser';
} else switch(array_shift($element))
{
case 'traefaeldning':
$page2 = 'traefaeldning';
break;
case 'beskaering':
$page2 = 'beskaering';
break;
default:
header('HTTP/1.1 404 Not Found');
$page2 = '404';
}
return $page2;
}
// https://stackoverflow.com/questions/16388959/url-rewriting-with-php
$path = ltrim($_SERVER['REQUEST_URI'], '/'); // Trim leading slash(es)
$elements = explode('/', $path); // Split path on slashes
if(empty($elements[0])) { // No path elements means home
$YdelseMenu = true;
$page = 'home';
} else switch(array_shift($elements)) // Pop off first item and switch
{
case 'ydelser':
$YdelseMenu = true;
$page = VisYdelser($elements); // passes rest of parameters to internal function
break;
case 'kontakt2':
$page = 'kontakt';
break;
default:
header('HTTP/1.1 404 Not Found');
$page = '404';
}
This might be a kaybord error but i cant find it. the error only seems to happen then 'VisYdelse()' is called and passes on to the 'switch'?
I hope someone can see what i do wrong. And maby som tips to optimise the code will be nice:)
EDIT i made a err log but but its empty (testet the log, end it do work)
There is no problem with the code.
The error was in the htaccess.
Changed FallbackResource index.php
to FallbackResource /index.php
Note the leading slash.
Related
I'm coding a CDN in PHP, it works this far, but I want to choose different versions by adding an '#' with the version behind it. How do I get the version String after the '#'.
Example: https://cdn.steven2105.de/jquery#3.3.0
<?php
$error = false;
$path = str_replace("/", "", $_SERVER['REQUEST_URI']);
switch($path) {
case "jquery":
$toOpen = "jquery/3.3.1/jquery.min.js";
break;
case "jquery#3.3.0":
$toOpen = "jquery/3.3.0/jquery.min.js";
break;
case "vue":
$toOpen = "vue/2.6.10/vue.min.js";
break;
case "bootstrap-css":
$toOpen = "bootstrap/4.3.1/bootstrap.min.css";
break;
case "bootstrap-js":
$toOpen = "bootstrap/4.3.1/bootstrap.min.js";
break;
case "baguettebox-css":
$toOpen = "baguettebox/1.11.0/baguettebox.min.css";
break;
case "baguettebox-js":
$toOpen = "baguettebox/1.11.0/baguettebox.min.js";
break;
case "popper":
$toOpen = "popper/1.14.7/popper.min.js";
break;
default:
$error = true;
break;
}
if (!$error) {
header("Location: https://cdn.steven2105.de/libs/$toOpen");
} else {
include_once "website.php";
}
EDIT: What should I do now? It doesn't work.
I have different libaries in the path https://cdn.steven2105.de/libs/ e.g. https://cdn.steven2105.de/libs/jquery with files in it such like https://cdn.steven2105.de/libs/jquery/3.3.1/jquery.min.js. I'd like to access these links with a shorter prefix like https://cdn.steven2105.de/jquery#3.3.1. If I use the '#' character it should redirect to the version in the directory https://cdn.steven2105.de/libs/jquery/ and if it's without the '#' it should redirect to the latest version.
What is the easiest way?
You can use explode function for this. It Outputs as an array and fetch it.
<?php
$URL = ' https://cdn.steven2105.de/jquery#3.3.0';
$Result = explode('#', $URL);
echo("<pre>");print_r($Result[1]);
?>
Instead of using PHP you could probably fix this with webserver configuration.
Rewrite rules in Apache (https://httpd.apache.org/docs/2.4/rewrite/remapping.html) should do the trick. I would expect Nginx to offer similar functionality.
If you use Apache or Nginx to do this for you it will be way faster as you won't have the overhead of running a PHP script.
I have the following files. The objective of this is to redirect to the correct news. For example:
localhost/tostadotv/esto-es-una-noticia-28.html
If I intentionally modify the url, for example:
localhost/tostadotv/esto-es-una-noticia-modificada-incorrecta-28.html
I should redirect myself to the correct news:
localhost/tostadotv/esto-es-una-noticia-28.html
However, it redirects me to this:
http://localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/localhost/tostadotv/esto-es-una-noticia-28.html
Where this error? Could you please help me thanks. Excuse my english I'm from Argentina I do not speak English
.htaccess
RewriteEngine On
RewriteRule ^.*-([0-9]+)\.html$ noticia.php?id_not=$1 [L]
noticia.php
<?php require_once("lib/connection.php"); ?>
<?php require_once("lib/functions.php"); ?>
<?php
fix_category_product_url();
?>
functions.php
function fix_category_product_url() {
$proper_url = get_proper_category_product_url(1);
if ( SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$proper_url);
exit();
}
}
function get_proper_category_product_url($id) {
$product_id = $_GET['id_not'];
$query = sprintf('SELECT titulo FROM noticias WHERE id_not = "%d"', mysqli_real_escape_string($GLOBALS['DB'], $product_id));
$restit = mysqli_query($GLOBALS['DB'], $query);
$noticia = mysqli_fetch_array($restit);
$proper_url = make_category_product_url($noticia['titulo'], $product_id, $id);
return $proper_url;
}
define('SITE_DOMAIN', 'localhost');
function _prepare_url_text($string) {
$NOT_acceptable_characters_regex = '#[^-a-zA-Z0-9_ ]#';
$string = iconv('UTF-8','ASCII//TRANSLIT',$string);
$string = preg_replace($NOT_acceptable_characters_regex, '', $string);
$string = trim($string);
$string = preg_replace('#[-_ ]+#', '-', $string);
return $string;
}
function make_category_product_url($product_name, $product_id, $ido) {
$clean_product_name = _prepare_url_text($product_name);
if ($ido == 0)
$url = strtolower($clean_product_name).'-'.$product_id.'.html';
else
$url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html';
return $url;
}
As said in the comments, the final solution for the asker was to add http:// to the defined SITE_DOMAIN constant.
Before
define('SITE_DOMAIN', 'localhost');
After
define('SITE_DOMAIN', 'http://localhost');
But there's more to it than just that. Let's focus on the following two functions:
function fix_category_product_url(){
$proper_url = get_proper_category_product_url(1);
if(SITE_DOMAIN.$_SERVER['REQUEST_URI'] != $proper_url){
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$proper_url);
exit();
}
}
function make_category_product_url($product_name, $product_id, $ido) {
$clean_product_name = _prepare_url_text($product_name);
if($ido == 0)
$url = strtolower($clean_product_name).'-'.$product_id.'.html';
else
$url = SITE_DOMAIN.'/tostadotv/'.strtolower($clean_product_name).'-'.$product_id.'.html';
return $url;
}
The idea here is that $proper_url actually ends up getting a value from make_category_product_url() because its result is returned by get_proper_category_product_url(). It makes sense because make_category_product_url() has more parameters and uses the other to get their values.
What's funny about this is that the else block of the second function doesn't always return a path, but rather a URL. The problem here is that such URL is given without a defined protocol, but starts with the domain name instead. This value is therefore mistaken as a path.
Now take a look at the first function: it ultimately redirects the user using header('Location: '.$proper_url);. As we discussed earlier, $proper_url is not always a path, so the protocol should be added somewhere in the code whenever a URL takes place instead of a path. That's where the actual solution comes in: adding http:// where SITE_DOMAIN is defined is one way to do this, because this constant is only used when a URL takes place. There are many other ways to do this, but this one is completely valid.
The below code snippet works great when run on localhost:3000 but stops working when I run it on localhost/dir/ and for every thing shows the default case result even for a /
<?php
// Grabs the URI and breaks it apart in case we have querystring stuff
$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2);
// Route it up!
switch ($request_uri[0]) {
// Home page
case '/':
echo "/ here\n";
break;
// About page
case '/about':
echo "about.php is opened from here\n";
break;
// Everything else
default:
header('HTTP/1.0 404 Not Found');
echo "404 is opened from here \n";
break;
}
?>
i am using this script to redirect users according to their IP.
But the problem is it redirects to homepage or only to URL i specify in the script. how can i just change the domain keeping the URL same? for example site.com/whateverpage redirected to site.au/whateverpage.
<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');
// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');
try {
$country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);
switch((string)$country) {
case 'AU':
$url = "http://www.site.au";
break;
case 'CA':
$url = "http://www.site.ca";
break;
default:
$url = "http://site.com";
}
if (strpos("http://$_SERVER[HTTP_HOST]", $url) === false)
{
header('Location: '.$url);
}
} catch (Exception $e) {
// Handle exception
}
?>
Add $_SERVER['REQUEST_URI'] to your redirection.
header("Location: $url/$_SERVER[REQUEST_URI]");
I have a form on my site and every time I submit this form..I get a 301 perm, then a 302 redirect, then another 302 redirect. My data is not submitting because of these redirects. I'm having no luck finding these redirects. There is no .htaccess file that is causing this in the root of the directory.
Where do I look to get rid of the redirects? Any tips would be appreciative.
My developer tools on submit goes to 301, 302, 302.
Here is the start of my form.
<form action="index.php?view=ticket_submit" method="POST" name="QContact" runat="vdaemon">
When this submits I have a case statement look for ticket_submit. It's not even getting to ticket_submit because of the var_dump and exit methods. It just goes back to the root index.php file.
private function ProcessView($type){
$ticket = new Ticket();
$ticket->CurrentUser = $this->CurrentUser;
$ticket->TicketUser = $this->CurrentUser;
$ticket->setBlankTicketHTML();
$title = 'New ROI';
$prior_count = count($this->Containers);
$_SESSION['vdaemon'] = '1';
if($_SESSION['al'] == 't' && $type['view'] == ''){
$type['view'] = 'summary';
}
switch($type['view']){
case 'ticket_edit':
$ticket->ticketid = (int)$this->Request['ticketid'];
$ticket->TicketUser = array();
$ticket->fillTicket();
if(($this->CurrentUser['userid'] != $ticket->TicketUser['userid']) && ($_SESSION['al'] == 't' || $_SESSION['al'] == 'r')){
$ticket->setViewTicketHTML();
$title = "Error: You can only Edit items you have created.";
$_SESSION['vdaemon'] = '0';
} else {
$ticket->setEditTicketHTML();
$title = 'Edit ROI';
$_SESSION['vdaemon'] = '1';
}
break;
case 'ticket_submit':
var_dump('test');
exit();
$ticket->ticketid = (int)$this->Request['ticketid'];
$ticket->TicketUser['useremail'] = $this->Request['useremail'];
$ticket->saveTicket(0);
$_SESSION['vdaemon'] = '0';
if($_REQUEST['viewquick'] == 'quick'){$quick = 'ticket_quick_view';}else{$quick = 'ticket_filed';}
if($_REQUEST['edit'] == 'yes') {
echo 'works';
}else {
header("Location: ?view=".$quick."&ticketid=".$ticket->ticketid);
echo 'workss';
}
exit;
break;
I ended up fixing it and this is what the cause was.
Three things needed to be fixed.
The first thing was that I needed to fix a reference to a vdaemon library. The path was all screwed up. After that I got rid of a header redirect in two places. One was in a separate folder and one was on my index.php which I totally missed (thought I checked it). It was doing 302 redirects because the headers were there but in different folders throughout the server and on the index.php.
Next time I'll know what to look for. Thanks for everyone's efforts here. :)
Make sure you look for this code first when dealing with these errors
header("Location: http://www.foo.com");