html - PHP redirect when URL is not requested through ajax (when on a wordpress page) -


i have wordpress website domain: hello.com "ajaxified". want know how redirect user hello.com if visit hello.com/ask, hello.com/cake or deeper using browser address bar.

even if go 2 degrees deeper hello.com/cake/make, want them stay @ root of website.

i using wordpress , first idea put redirect script @ header, ended making infinite redirect loop.

how can redirect users homepage without redirecting ajax requests homepage well?

[if ask why disallow them go deeper, answer is: i using ajax load deeper pages , else div]

by adding short php script somewhere before else, able check how pages loaded using condition below.

if(strtolower($_server['http_x_requested_with']) != 'xmlhttprequest') 

it allowed me check through php if through ajax or not.

the value of $_server['http_x_requested_with'] xmlhttprequest when request ajax request.

for case, how used it:

if(strtolower($_server['http_x_requested_with']) != 'xmlhttprequest') {     header("location: http://". $_server['http_host']."/"); } 

which check if request done through ajax, , redirect homepage/main screen of website/web app if not.

i got idea after reading this post


note: usage of http_x_requested_with depends on javascript framework using, if sets header. using jquery , you. $_server not entirely part of php depends if server passes variable php. if on nginx server , doesn't work you, can try this: passing http_x_requested_with nginx php

hope helps others!


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -