Someone may saw the similar question like this,but I can't find the complete resolution about this.
now here has a link www.example.com/test/index.php, if I add an slash to the end, like www.example.com/test/index.php/,it would get
redirected you too many times
so I use this code to filter the url:
if ($_SERVER['REQUEST_URI'] != "/" && preg_match('{/$}',$_SERVER['REQUEST_URI'])) {
header ('Location: '.preg_replace('{/$}', '', $_SERVER['REQUEST_URI']));
exit();
}
it would turn index.php/ into index.php
but now if I just go www.example.com/test/ it would get
redirected you too many times.
so now my question is, how can I let them both work?
P.S. My server can't use .htacess
Related
I use this structure for my post URL in my Wordpress website.
Domain.com/%post_id%/%postname%
if you delete the /%postname% part you will go to the same page?
i mean
Domain.com/post_id/postname = domain.com/post_id
Is there any way that when a user visit domain.com/post_id gets redirect to Domain.com/post_id/postname ?
Sure.
I suggest adding an action hook to an early event, maybe wp?
add_action("wp", function() {
if((is_single() || is_page()) && preg_match("!^/\d+/?$!", $_SERVER["REQUEST_URI"])) {
http_response_code(301);
header("Location: " . get_permalink());
exit;
}
});
This will run on every request and check whether that request is a) a page or post AND b) the REQUEST_URI (=local part of the URL) starts with a slash, followed by numbers, followed by an optional final slash, and nothing else. If these two conditions are met, it will return a HTTP 301 response code ("Redirect Permanent") with a Location header indicating the correct URL.
Put it into your functions.php.
It's interesting that WordPress will automatically redirect /123/postti to /123/posttitle, but it won't to the same for /123. /123/ will be redirected to /123. The code snippet I posted will take care of /123 and /123/, while wordpress takes care of those with a partial title.
I've had a previous question answered on how to add a / to my url and redirecting it internally. The answer I got worked great, see here but i've stumbled on a new problem.
When you recover your password you are forced to change it:
if (logged_in() === true) {
if ($current_file !== 'changepassword.php' && $user_data['password_recover'] == 1) {
header('Location: changepassword.php?force');
exit();
}
}
When you're on the index page, and are forced to change your password, the URL goes from mysite.com//index/ to mysite.com//index/changepassword/?force which obviously does not exist.
When I use ?force without changepassword.php it gets a "this page has an this page has redirect loop" warning, although it did work for other things like ?success.
Base href="/" also doesn't have any effect.
Any ideas?
My guess of your problem is that you replace a "terminating" .php with /. So /index.php turns into /index/. If you now redirect to changepassword.php?force, you will end up with /index/changepassword/?force.
But as the script changepassword.php likely is located at the same level as index.php, you have to use a host relative redirection, instead of a path relative redirection:
header('Location: /changepassword.php?force');
Please note the leading / in the location target.
I am trying to use a php script that redirects visitors based on certain criteria. I use the script succesfully on an apache server however, I am experimenting with nginx and php-fpm and the same script doesn't seem to be working as it should.
header("Location: $url");
exit();
The strange thing is it appears to be appending the URL I am trying to redirect to to the original URL so the URL it tries to forward to looks like:
originaldomain.com/redirectdomain.com.
Has anybody ever come across this before where it as appending the redirect domain to the original URL instead of redirecting straight to it?
Please let me know if you need any further information to help.
You need to make sure the URL has http:// at the beginning of it, otherwise it thinks it's going to a path on your domain, and not a redirect to an actual site.
$url has to begin with http:// or https://
if (strpos($url, 'http') === 0 ) {
$newurl = $url;
} else {
$newurl = "http://" . $url;
}
Then just use $newurl in you header request :)
If the above answers are not yet fixed header redirect, you need to check in your php.ini file output_buffering on or off or set limit.
output_buffering = On
So I made a script so that I can just use includes to get my header, pages, and then footer. And if a file doesnt exist a 404. That all works. Now my issue is how I'm supposed to get the end of the url being the page. For example,
I want to make it so that when someone goes to example.com/home/test, it will automatically just include test.php for example.
Moral of the story. How to some how get the page name. And then use it to "mask" the end of the page so that I don't need to have every URL being something.com/home/?p=home
Heres my code so far.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_dc.php');
include($_SERVER['DOCUMENT_ROOT'].'/home/lib/php/_home_fns.php');
$script = $_SERVER['SCRIPT_NAME']; //This returns /home/index.php for example =/
error_reporting(E_ALL);
include($_SERVER['DOCUMENT_ROOT'].'/home/default/header.php');
if($_GET["p"] == 'home' || !isset($_GET["p"])) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/home.php');
} else if(file_exists($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php')) {
include($_SERVER['DOCUMENT_ROOT'].'/home/pages/'.$_GET["p"].'.php');
} else {
include($_SERVER['DOCUMENT_ROOT'].'/home/default/404.php');
}
include($_SERVER['DOCUMENT_ROOT'].'/home/default/footer.php');
?>
PHP by itself wouldn't be the best choice here unless you want your website littered with empty "redirect" PHP files. I would recommend looking into the Apache server's mod_rewrite module. Here are a couple of guides to get you started. Hope this helps!
The simplest way would be to have an index.php file inside the /home/whatever folder. Then use something like $_SERVER['PHP_SELF'] and extract the name if you want to automate it, or since you are already writing the file yourself, hardcode it into it.
That however looks plain wrong, you should probably look into mod-rewrite if you are up to creating a more complex/serious app.
I would also recommend cakePHP framework that has the whole path-to-controller thing worked out.
I have a site I'm working on with a /landing-page/ folder, and I'll be making a number of landing pages. I'd like to be able to put urls that have /lp/ instead of /landing-page/ in advertisements, and have any url with /lp/ in it replaced with /landing-page/.
Thus:
www.site.com/lp/ipad-inspections
would automatically redirect to:
www.site.com/landing-page/ipad-inspections
I would like to do this without having a /lp/ folder and a second page corresponding to each landing page. My thought is to have the 404 page check the url and redirect, but I can't seem to get the following to work:
<?php /* Automatic redirect for landing pages */
$current_loc = $_SERVER['REQUEST_URI'];
$short_lp = '/lp/';
$long_lp = '/landing-page/';
if (strpos($current_loc, $short_lp)) {
$current_loc = str_replace($short_lp, $long_lp, $current_loc, 1);
header("location: ".$current_loc);
}
What am I doing wrong here, that my page is coming up blank? I have narrowed it down to the first line in my if statement, which is crashing my page.
If there a better way to do this with apache? Some line I can put in htaccess, maybe?
RedirectPermanent /lp /landing-page
in your .htaccess should do the trick. Best to do this sort of unconditional redirect BEFORE it reaches the PHP stage. It's essentially a "free" operation in Apache, and saves your server the whole parse/compile/execute PHP stages.
Keep it as you've done it but include the 301 permanently moved header.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
Also try referencing your URL absolutely, so as to include http://www.domain.com/
Update
If you want to redirect with htaccess try
redirect 301 /lp/landing-page/ http://www.domain.com/new.html