Php get website url after https:// or www. or subdomain - php

I want to get the correct URL with PHP without any error, my links example:
https://example.com/
https://example.com/search/
https://example.com/search/?q=test
https://it.example.com/
https://it.example.com/search/
https://it.example.com/search/?q=test
so i want to get all link if is https://example.com/ show example.com if is https://example.com/search/ show example.com/search/ if is https://it.example.com/search/?q=test show example.com/search/?q=test etc.. without any error. thanks

Looks like you need $_SERVER['REQUEST_URI']:
$link = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Have a look over the PHP documents too: http://php.net/manual/en/reserved.variables.server.php
This will return the HTTP Host without https, and will also get you the request_uri with query strings etc.
parse_url() will also give you each element, and then you can build up the string you need:
http://php.net/manual/en/function.parse-url.php

Related

Get domain address of calling webpage in PHP

I want to apply check only a specific website domain can hit my site. I tried $_SERVER['HTTP_REFERER'] but i am getting the full url.But i just want the domain address like www.example.com not all the url. I tried to explode but in the start i am getting http:\\ so when i explode on \ this .It explode the http section as well. Is there any other way so i can get on the domain url.
if the url is http://www.example.com/test.php i only want http://www.example.com. I have also used explode but explode function explode the http/ section first.I want the url of another website who's hitting my website
$hitting_url=$_SERVER['HTTP_REFERER'];
$site_url=parse_url($hitting_url, PHP_URL_HOST);
if($site_url != 'www.example.com')
{
header('Location:'.$hitting_url);
exit();
}
I have fix the issue with parse_url.

Using .htaccess file for good looking urls

What I'm trying to do is make my website's urls look prettier to the users.
For example I have this link in the index.php file in a href tags:
index.php?v=class&id=5
And I want the user to see in the address bar this:
www.mysite.com/class/5
I have tried using this code:
RewriteRule /([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?v=$1&id=$2
But I found out that it does the opposite. It makes the good looking urls turn into bad ones. It would take the www.mysite.com/class/5 and show this: www.mysite.com/index.php?v=class&id=5. What should I do?
You're almost there.
Don't use R in your RewriteRule. R is an external redirect (it tells the browser to redirect, so the URL changes). Instead, remove the R to do an internal redirect, that way Apache can still parse the query string normally, but the user sees the pretty URL (the URL doesn't change in the browser).
Have you tried these yet?
Here's some links I found when seraching Google:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.nouveller.com/quick-tips/quick-tip-6-how-to-write-clever-pretty-urls-with-htaccess/

.htaccess mod_rewrite to create clean URLs

I have a data driven site that passes information to determine what the next page should show using the $_GET parameter.
I want the URL's to look nicer and be structured simply.
I have been reading about mod_rewrite but so far failed to implement it.
<?php $post = $_GET['ID']; ?>
<?php $loca = $_GET['loca']; ?>
This is taken from the URL to work out what table we want and what post ID. The URL at the moment is index.php?ID=4&loca=Pages
How would I make this work if it were instead. /pages/(the name column of the post of this ID).
This should do the internal rewrite:
RewriteEngine On
RewriteRule ^pages/(\d+)/ /index.php?ID=$1&loca=pages
It rewrites any url starting with pages/(some number)/ to the result. You should probably add some server side logic as well to do a 302 redirect if the url isn't exactly /pages/id/(Name that matches id)/. You can use $_SERVER['REQUEST_URI'] to get the string and then compare it to the string that it should be and do a redirect if it doesn't match.
Just like if you go to: https://stackoverflow.com/questions/11057691/This+is+not+the+title
You get redirected to the version with the correct title. You should also update the links you have around your site to use the new url format.
There are a lot of examples of how to do this on google.
Tutorial: http://wettone.com/code/clean-urls
Mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

How to extract URL parameters on 404 Error in PHP

I have my htaccess redirecting to a error404.php but I want that PHP file to be able to extract the original url parameters.
For example:
http://mywebsite.com/unknownfile.php?param1=value1&param2=value2
I've tried $_GET['param1'] but that's empty. The $_SERVER['PHP_SELF'] just shows error404.php
Many thanks in advance.
You can access original URL via $_SERVER["REQUEST_URI"].
For example: requesting URL /hihi/meow?key=rumba which does not exists. The $_SERVER["REQUEST_URI"] will have that string, which you can parse with parse_url() function to split into parts and use other functions (like explode() to get to individual query strung parameters.
If you redirect this info will only be present in $_SERVER['HTTP_REFERER']

Fix links addresses on websites HTML code

i have been working on one tool lately. It grabs all the link addresses from the website.
My problem is that links in html code sometimes is different:
/index.php
index.php
http://www.website.com/index.php
I need to make all links same:
/index.php -> http://www.website.com/index.php
index.php -> http://www.website.com/index.php
http://www.website.com/index.php -> http://www.website.com/index.php
Thanks for help.
Using preg_replace to fix relative urls
Requires:
$domain = the subject sites domain
$path = the document or string you are looking for relative links with in.
Returns:
$url = the doument or string with the links within it converted to proper urls with the domain given.
Code:
$url = preg_replace('<a\shref="([\/\?\w\.=\&]+)"([\s]rel="(\w+)")*>/', '<a href="http://{$site_domain}$1" rel="$3">' $path)
good luck, let me know how it goes.
Welcome to GoogleOverflow.com.
Here is the complete tutorial for parsing links in HTML using PHP and regex: http://www.the-art-of-web.com/php/parse-links/
Here's a function which will return the absolute URL given the base (current) URL and a relative one.
You need to check for the existence of a base tag. If you find it, it specify the base URL (otherwise, the base URL is the same path the browser points to, up to the last /).

Categories