Connecting Tech Pros Worldwide Help | Site Map

Change request URL

bgold12
Guest
 
Posts: n/a
#1: Nov 14 '08
Hey, I want to standardize the URL that users use to get to my site.
For example, I want to change:

http://example.com/

to:
http://www.example.com/index.php

and I want to change:

http://example.com/page.php

to:
http://www.example.com/page.php

Basically I want to make sure every page has the www component of the
url, and I want to add the index.php to the home page url if it's not
present. How can I do this either from PHP or using a different method?
Jerry Stuckle
Guest
 
Posts: n/a
#2: Nov 14 '08

re: Change request URL


bgold12 wrote:
Quote:
Hey, I want to standardize the URL that users use to get to my site.
For example, I want to change:
>
http://example.com/
>
to:
http://www.example.com/index.php
>
and I want to change:
>
http://example.com/page.php
>
to:
http://www.example.com/page.php
>
Basically I want to make sure every page has the www component of the
url, and I want to add the index.php to the home page url if it's not
present. How can I do this either from PHP or using a different method?

You can't do it from PHP or any other server-side language (at least not
easily) - that's way too late. You need to do it in your server
configuration. Try the appropriate web server newsgroup, i.e.
alt.apache.configuration.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
J.O. Aho
Guest
 
Posts: n/a
#3: Nov 14 '08

re: Change request URL


Jerry Stuckle wrote:
Quote:
bgold12 wrote:
Quote:
>Hey, I want to standardize the URL that users use to get to my site.
>For example, I want to change:
>>
>http://example.com/
>>
>to:
>http://www.example.com/index.php
>>
>and I want to change:
>>
>http://example.com/page.php
>>
>to:
>http://www.example.com/page.php
>>
>Basically I want to make sure every page has the www component of the
>url, and I want to add the index.php to the home page url if it's not
>present. How can I do this either from PHP or using a different method?
>
>
You can't do it from PHP or any other server-side language (at least not
easily) - that's way too late. You need to do it in your server
configuration. Try the appropriate web server newsgroup, i.e.
alt.apache.configuration.
>
Using mod_rewrite, the Apache will use header redirections, you can do the
same in php too, using $_SERVER values to check if the user made the request
correctly according to the OP post and then in those cases it's not correct,
use header() to redirect to the url which they should have written from the
begining. Don't forget to take care of $_GET/$_POST values and include those
to the header too.

I can say I personally against redirection for normal request, just look at
php.net, do a request for php,net/header and you end up somewhere else, in my
case always at a slow performing server, many times it goes faster to open a
new tab and write uk2.php.net/header

--

//Aho
=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
Guest
 
Posts: n/a
#4: Nov 14 '08

re: Change request URL


bgold12 escribió:
Quote:
Hey, I want to standardize the URL that users use to get to my site.
For example, I want to change:
>
http://example.com/
>
to:
http://www.example.com/index.php
>
and I want to change:
>
http://example.com/page.php
>
to:
http://www.example.com/page.php
>
Basically I want to make sure every page has the www component of the
url, and I want to add the index.php to the home page url if it's not
present. How can I do this either from PHP or using a different method?
In PHP, you just need to check the URL in every page and perform an HTTP
redirection if it doesn't match your standard. You can lookup for
available variables with print_r($_SERVER) and then generate a header:

header('Location: http://www.example.com/...........', true, 301);

The 301 HTTP status code means "Moved Permanently". I suppose you have a
common 'settings.php' or whatever where you can place the call to these
checks. Don't forget the query string in the URL (thes post parameters
will be lost though).

However, it's normally easier to do all this with the web server itself.
Apache, for instance, allows you to specify the www check with this syntax:

# Not tested!
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com/$0 [R=301,QSA,L]

Note this is *not* PHP. I suppose other web servers have similar features.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
rf
Guest
 
Posts: n/a
#5: Nov 14 '08

re: Change request URL



"J.O. Aho" <user@example.netwrote in message
news:6o4jb5F1haf0U1@mid.individual.net...
Quote:
Jerry Stuckle wrote:
Quote:
>bgold12 wrote:
Quote:
>>Hey, I want to standardize the URL that users use to get to my site.
>>For example, I want to change:
>>>
>>http://example.com/
>>>
>>to:
>>http://www.example.com/index.php
>>>
>>and I want to change:
>>>
>>http://example.com/page.php
>>>
>>to:
>>http://www.example.com/page.php
>>>
>>Basically I want to make sure every page has the www component of the
>>url, and I want to add the index.php to the home page url if it's not
Quote:
Using mod_rewrite, the Apache will use header redirections,
mod_rewrite does not use header redirections, it rewrites the existing
request URL before the server gets a look at it.
Quote:
you can do the
same in php too, using $_SERVER values to check if the user made the
request
correctly according to the OP post and then in those cases it's not
correct,
use header() to redirect to the url which they should have written from
the
begining. Don't forget to take care of $_GET/$_POST values and include
those
to the header too.
Nope.

www happens (or should happen) at the DNS level, way before apache gets a
look in and most certainly way before PHP has a go.

This is *not* a PHP issue, it is about domain name servers plus a bit of
HTTP server setup. The former to ensure there is a CNAME record to alias
www.example.com to example.com and the latter to ensure index.php is the
default file to be served when there is no file mentioned in the URL.




Geoff Berrow
Guest
 
Posts: n/a
#6: Nov 14 '08

re: Change request URL


Message-ID: <UhaTk.14938$sc2.4392@news-server.bigpond.net.aufrom rf
contained the following:
Quote:
>www happens (or should happen) at the DNS level, way before apache gets a
>look in and most certainly way before PHP has a go.
>
>This is *not* a PHP issue, it is about domain name servers plus a bit of
>HTTP server setup. The former to ensure there is a CNAME record to alias
>www.example.com to example.com and the latter to ensure index.php is the
>default file to be served when there is no file mentioned in the URL.
When I set up domains on my VPS with cpanel/WHM it's automatic. Quite a
handy feature.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk
J.O. Aho
Guest
 
Posts: n/a
#7: Nov 14 '08

re: Change request URL


rf wrote:
Quote:
"J.O. Aho" <user@example.netwrote in message
news:6o4jb5F1haf0U1@mid.individual.net...
Quote:
>Jerry Stuckle wrote:
Quote:
>>bgold12 wrote:
>>>Hey, I want to standardize the URL that users use to get to my site.
>>>For example, I want to change:
>>>>
>>>http://example.com/
>>>>
>>>to:
>>>http://www.example.com/index.php
>>>>
>>>and I want to change:
>>>>
>>>http://example.com/page.php
>>>>
>>>to:
>>>http://www.example.com/page.php
>>>>
>>>Basically I want to make sure every page has the www component of the
>>>url, and I want to add the index.php to the home page url if it's not
>
Quote:
>Using mod_rewrite, the Apache will use header redirections,
>
mod_rewrite does not use header redirections, it rewrites the existing
request URL before the server gets a look at it.
From Apache.org about mod_rewrite:

This module operates on the full URLs (including the path-info part) both in
per-server context (httpd.conf) and per-directory context (.htaccess) and can
even generate query-string parts on result. The rewritten result can lead to
internal sub-processing, external request redirection or even to an internal
proxy throughput.

Surely you don't have to redirect with mod_rewrite, but I was advocating for a
similarity.

Quote:
Quote:
>you can do the
>same in php too, using $_SERVER values to check if the user made the
>request
>correctly according to the OP post and then in those cases it's not
>correct,
>use header() to redirect to the url which they should have written from
>the
>begining. Don't forget to take care of $_GET/$_POST values and include
>those
>to the header too.
Quote:
www happens (or should happen) at the DNS level, way before apache gets a
look in and most certainly way before PHP has a go.
This is *not* a PHP issue, it is about domain name servers plus a bit of
HTTP server setup. The former to ensure there is a CNAME record to alias
www.example.com to example.com and the latter to ensure index.php is the
default file to be served when there is no file mentioned in the URL.
I'm not going into what the OP should have done, but suggesting how he can use
PHP to do the whole thing.


--

//Aho
Jerry Stuckle
Guest
 
Posts: n/a
#8: Nov 14 '08

re: Change request URL


J.O. Aho wrote:
Quote:
rf wrote:
Quote:
>"J.O. Aho" <user@example.netwrote in message
>news:6o4jb5F1haf0U1@mid.individual.net...
Quote:
>>Jerry Stuckle wrote:
>>>bgold12 wrote:
>>>>Hey, I want to standardize the URL that users use to get to my site.
>>>>For example, I want to change:
>>>>>
>>>>http://example.com/
>>>>>
>>>>to:
>>>>http://www.example.com/index.php
>>>>>
>>>>and I want to change:
>>>>>
>>>>http://example.com/page.php
>>>>>
>>>>to:
>>>>http://www.example.com/page.php
>>>>>
>>>>Basically I want to make sure every page has the www component of the
>>>>url, and I want to add the index.php to the home page url if it's not
>>Using mod_rewrite, the Apache will use header redirections,
>mod_rewrite does not use header redirections, it rewrites the existing
>request URL before the server gets a look at it.
>
From Apache.org about mod_rewrite:
>
This module operates on the full URLs (including the path-info part) both in
per-server context (httpd.conf) and per-directory context (.htaccess) and can
even generate query-string parts on result. The rewritten result can lead to
internal sub-processing, external request redirection or even to an internal
proxy throughput.
>
Surely you don't have to redirect with mod_rewrite, but I was advocating for a
similarity.
>
It's the most efficient way to do it.
Quote:
>
Quote:
Quote:
>>you can do the
>>same in php too, using $_SERVER values to check if the user made the
>>request
>>correctly according to the OP post and then in those cases it's not
>>correct,
>>use header() to redirect to the url which they should have written from
>>the
>>begining. Don't forget to take care of $_GET/$_POST values and include
>>those
>>to the header too.
>
Quote:
>www happens (or should happen) at the DNS level, way before apache gets a
>look in and most certainly way before PHP has a go.
>This is *not* a PHP issue, it is about domain name servers plus a bit of
>HTTP server setup. The former to ensure there is a CNAME record to alias
>www.example.com to example.com and the latter to ensure index.php is the
>default file to be served when there is no file mentioned in the URL.
>
I'm not going into what the OP should have done, but suggesting how he can use
PHP to do the whole thing.
>
>
mod_rewrite is much more efficient to do it than a header() redirect.
It's easier, also.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Tim Greer
Guest
 
Posts: n/a
#9: Nov 14 '08

re: Change request URL


"Ãlvaro G. Vicario" wrote:
Quote:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com/$0 [R=301,QSA,L]
I know you said this wasn't tested, but it was close. The conditional
is correct, but the rule used $0 (there is no $0, it starts at $1), and
there was nothing captured for $1, unless you use ^(.*)$ (or something
similar). The example should work now, but yeah, not PHP.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
J.O. Aho
Guest
 
Posts: n/a
#10: Nov 14 '08

re: Change request URL


Jerry Stuckle wrote:
Quote:
mod_rewrite is much more efficient to do it than a header() redirect.
It's easier, also.
I do agree on that, but even better if you can be without both.



--

//Aho
Closed Thread