application variables in wordpress

Hello experts
I m new user of  Wordpress and php with experience in coldfusion server language.
I m asking if there is a code to redirect to the home page using the homeurl as a variable.
(i wand to use this in index.php files inside subfolders for security reasons)
I have the following code:
<?php header('Location: https://mysitedomainname.com');  
i need the https://mysitedomainname.com to be a variable

Any help?
LVL 2
PanosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I think you are better off using a plug in for this. I use https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

If you want to create your own redirects, use the functions.php in your child theme https://developer.wordpress.org/reference/functions/wp_redirect/ and you can build your logic there.
0
PanosAuthor Commented:
Hi
thank you for your post.
1) I did tried the first option and made tests with a plugin but it is not working as i want.
I think it will be easier for me to write one line in each index.php file i want.
in coldfusion it  is :
<cflocation url = "#application.homeurl#">
where homeurl  application.homeurl is been defined in my application.cfc/cfm page.
There must be somthing equal in php too.

2) How can i use the second option in my index.php file?
0
Shaun VermaakTechnical SpecialistCommented:
Depending on where, you can user [URL_Base] with URL ShortCodes plugin. Works with latest WordPress
https://wordpress.org/plugins/url-shortcodes/
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

PanosAuthor Commented:
Thank you both for your help.
This code is doing my job:
<?php header("Location:https://". $_SERVER['SERVER_NAME']); ?>
0
Shaun VermaakTechnical SpecialistCommented:
Then you can get HTTP/HTTPS with $_SERVER['HTTPS']
if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {
    //https
}
else
{
    //http
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PanosAuthor Commented:
Full code would be:
<?php if ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) {
      header("Location:https://". $_SERVER['SERVER_NAME']);
}
else
{
       header("Location:http://". $_SERVER['SERVER_NAME']);
}
?>

Is working !!
1
PanosAuthor Commented:
Thank you for your help.
Scott Fell solution was not really doing what i needed but it is very good to know about this.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The code you are working on will work as far as a redirect. However, keep in mind when you created a CF site, you probably used physical folders and placed index files in each.  Wordpress is really an mvc framework. Instead of physical folders, you are using routes.

Can I ask what is the underlying thing you are trying to do? obfuscate?  What you want may already be available without reinventing the wheel.
0
PanosAuthor Commented:
Hi
I wrote in the question that i m new in wordpress and php and ofcourse i don t want to reinventing the wheel.
I saw in my wp-content/themes folder an index.php file with this code :
<?php
// Silence is golden.
Typing the addres in the url i got ofcourse a blank page.
That s why i was trying to find a solution like i would do in coldfusion.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I see. That is a non-issue. Nothing to worry about for security.   If the file is blank there is no harm possible. It will also not be indexed in any way because you will not have any links to that page.  It's best to leave those pages as is and not muck with them. The only place to add custom code is in your theme functions.php page or a child theme.  

Using a 301 redirect would have been the better solution and you can do that either using htaccess or web.config if windows. But that is also what the plug ins do such as the one I pointed to. You can add a url and then redirect. But again, there is really no need to do this.  

Any other URI's in your site are created by routing everything through the main index page and using a redirect that is generated by your set up in settings -> permalinks.  yoursite.com/wp-admin/options-permalink.php
0
PanosAuthor Commented:
Thank you very much for all the info. It is good for me to know.
I don t know why i could not get it to work with the plugin. Maybe i will try again. All these plugins do really great work in Worpress but i m not used to use plugins until now. I was most writing the code myself in coldfusion.
1
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
What you are experiencing is very common for anybody that is coming from doing more hand coding / procedural programming.

If you really want to keep that code, you will want to make an adjustment so it is a 301 redirect. http://php.net/manual/en/function.header.php but much better to just create your redirect in htaccess or web.config.

Good luck!
0
PanosAuthor Commented:
Thank you again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.