PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 2, 2022

[FIXED] How to do a redirect in php from a root url to another without getting a redirect error

 July 02, 2022     php, xampp     No comments   

Issue

currently I am trying to make my own website that functions in a way like a wiki. I am using the XAMPP installer to create it, and I formatted the index.php file to redirect to a request file without any errors: http://localhost/site/request.php

The request file, as the name suggests, is the file where a request is made in the url, such as a value, etc, which would correspond to what file/page the user is looking for. However one thing that I would like to do is if the request is empty, it will redirect to the main page: http://localhost/site/request.php?Main_Pages:Main_Page Main_Pages being the type of page that the user wants to see, while Main_Page being the specific page of that type. Anyways, I tried doing this by inputting this piece of code in the request.php file:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if ($url = 'http://localhost/site/request.php') {
  header('Location: http://localhost/site/request.php?Main_Pages:Main_Page');
  exit;
}

However when I try reloading the page, it redirects to that address successfully, but it gives me the browser error: ERR_TOO_MANY_REDIRECTS

If you guys could help me that would be great, Thanks.


Solution

Is the configuration set up on XAMPP correctly to allow redirects? Can you share the site/request.php code to also show us what is happening on that page?

One more thing, your code says:

...
if ($url = 'http://localhost/site/request.php') {
...

which will be TRUE every time. You only have one =, it needs to be:

...
if ($url == 'http://localhost/site/request.php') {
...

if you plan on checking the actual value.



Answered By - Justin
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing