Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

change page url according to id

25 views
Skip to first unread message

adam

unread,
Jul 20, 2015, 2:08:31 AM7/20/15
to
hello there I have a application where admin enter page title and description and enter and that value goes in to database and then I retrieve that value and show that in the page.

Now the problem I have is that page have a url like /page.php?id=51 but I want it to be like /contact us or /about us.

wordpress has a same sysem it has a page.php and it is a default template for pages and every page has different tile.

Gordon Burditt

unread,
Jul 20, 2015, 4:14:56 AM7/20/15
to
Web servers like Apache tend to have some configuration directives
that allow you to manipulate URLs before they are processed. For
example, you could alias /contact_us to /page.php?id=51 .

Another possibility is to alias anything under /document/ ...
to be handled by a particular CGI (which could be a PGP page).
The PGP page could look at the tail of the URL as well as
the other parts of it, and do what was needed to simulate
a whole tree of pages. For example, it could look up the tail
of the URL (not including the variables) in a database and
use the information there to construct the page.

A number of genealogy websites pretend to have a large
tree of URLs like:

http://genealogy.example.com/genealogy/person_page/39273
http://genealogy.example.com/genealogy/family_page/22612

which actually have one or a small number of CGIs handling
anything under http://genealogy.example.com/genealogy/ .
It can also handle multiple site names which refer to independent
genealogy trees. The links intentially follow the forms above,
rather than a bunch of variables passed to the database.

apoorv....@gmail.com

unread,
Jul 20, 2015, 6:06:45 AM7/20/15
to
Thanks for the answer gordon I know we change individual page url with htaccess Apache file but I want it to be dynamic like if the user enter title of the page portfolio the page url should be /portfolio.

Richard Damon

unread,
Jul 20, 2015, 8:29:21 AM7/20/15
to
On 7/20/15 6:06 AM, apoorv....@gmail.com wrote:

>
> Thanks for the answer gordon I know we change individual page url
> with htaccess Apache file but I want it to be dynamic like if the
> user enter title of the page portfolio the page url should be
> /portfolio.>

The two ways I have seen this done is to use rewrite to map ALL (or a large set) of URLS to all map to something like index.php, and index.php check the URL it was called by to figure out what page to display (checking the mapping in the database).

A second way is to trap the 404 error and have that handler do this sort of operation.

The first is a bit faster, but a bit harder to have pages that bypass this mapping (if you want special code to process that page). The second handles this automatically, but does make the web server do a bit more work to redirect to the 404 error page.

Denis McMahon

unread,
Jul 20, 2015, 8:49:18 PM7/20/15
to
On Mon, 20 Jul 2015 03:06:30 -0700, apoorv.kanungo wrote:

> Thanks for the answer gordon I know we change individual page url with
> htaccess Apache file but I want it to be dynamic like if the user enter
> title of the page portfolio the page url should be /portfolio.

Are you referring to the url used to make the request, or the page
location shown in the browser after the request has been served?

The browser address bar will show the actual url fetched, which means you
need to have /pages/page.php?id=n redirect for each valid n (and don't
forget to trap invalid n) to something like /docs/portfolio, and then
process all requests for /docs/* in code that looks at the request header
and calculates which content to return (and again has a sensible default
if * doesn't match a valid value).

In apache, the configuration directive is:

AliasMatch ^/docs/.* /pages/page.php
AliasMatch ^/docs /pages/page.php

and then in /pages/page.php, you need to:

a) look at $_GET['id'] to see if there is an id value, test if it's
valid, and if it is redirect to /docs/portfolio (or whatever); or

b) If there's no $_GET['id'], look at $_SERVER['REQUEST_URI'] and see if
the uri matches /docs/portfolio (or whatever), and if it does, serve the
relevant page; or

c) send a default page because no valid data for a specific page request
was received.

Now a browser can request /pages/page.php?id=x and will get /docs/
documentname, where first of all /pages/page.php redirects to /docs/
documentname based on x, and then /pages/page.php serves content based on
documentname that appears from the uri /docs/documentname

The browser never realises that both requests are being served by the
same file, because the redirected uri is handled by the alias in the
server to point back at pages.php, the browser just follows the redirect
and asks the server for /docs/documentname

There may be other and better ways to do this, but I haven't found them
yet. I haven't looked very hard.

--
Denis McMahon, denismf...@gmail.com

Lew Pitcher

unread,
Jul 21, 2015, 12:28:59 PM7/21/15
to
On Monday July 20 2015 20:47, in comp.lang.php, "Denis McMahon"
<denismf...@gmail.com> wrote:

> On Mon, 20 Jul 2015 03:06:30 -0700, apoorv.kanungo wrote:
>
>> Thanks for the answer gordon I know we change individual page url with
>> htaccess Apache file but I want it to be dynamic like if the user enter
>> title of the page portfolio the page url should be /portfolio.
>
> Are you referring to the url used to make the request, or the page
> location shown in the browser after the request has been served?
>
> The browser address bar will show the actual url fetched, which means you
> need to have /pages/page.php?id=n redirect for each valid n (and don't
> forget to trap invalid n) to something like /docs/portfolio, and then
> process all requests for /docs/* in code that looks at the request header
> and calculates which content to return (and again has a sensible default
> if * doesn't match a valid value).
>
> In apache, the configuration directive is:
>
> AliasMatch ^/docs/.* /pages/page.php
> AliasMatch ^/docs /pages/page.php
>
[snip]
> There may be other and better ways to do this, but I haven't found them
> yet. I haven't looked very hard.

The Apache's mod_alias[1] and mod_rewrite[2] modules will also do this

Assuming that the request URL is something like
http://some.example.com/portfolio

then the mod_alias rule might look like
RedirectMatch "^/portfolio" "/page.php?name=portfolio"

or the mod_rewrite rule might look like
RewriteRule "^/portfolio" "/page.php?name=portfolio"

[1] http://httpd.apache.org/docs/2.4/mod/mod_alias.html

[2] http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.4/rewrite/remapping.html

HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Thomas 'PointedEars' Lahn

unread,
Jul 21, 2015, 4:08:32 PM7/21/15
to
Lew Pitcher wrote:

> On Monday July 20 2015 20:47, in comp.lang.php, "Denis McMahon"
> <denismf...@gmail.com> wrote:
>> The browser address bar will show the actual url fetched, which means you
>> need to have /pages/page.php?id=n redirect for each valid n (and don't
>> forget to trap invalid n) to something like /docs/portfolio, and then
>> process all requests for /docs/* in code that looks at the request header
>> and calculates which content to return (and again has a sensible default
>> if * doesn't match a valid value).
>>
>> In apache, the configuration directive is:
>>
>> AliasMatch ^/docs/.* /pages/page.php
>> AliasMatch ^/docs /pages/page.php
>>
> [snip]
>> There may be other and better ways to do this, but I haven't found them
>> yet. I haven't looked very hard.
>
> The Apache's mod_alias[1] and mod_rewrite[2] modules will also do this
>
> Assuming that the request URL is something like
> http://some.example.com/portfolio
>
> then the mod_alias rule might look like
> RedirectMatch "^/portfolio" "/page.php?name=portfolio"

This will *redirect* everything starting with “portfolio”. It can be
simplified to, thereby improved (unless you really want to redirect
everything, which is doubtful) to

Redirect "/portfolio" "/page.php?name=portfolio"

mod_redirect, _not_ mod_alias, is the proper way here because with mod_alias
you map URL-references to the *server’s filesystem* instead (possibly
*above* the DOCUMENT_ROOT), and hopefully there is not going to be a
publicly accessible *directory* with the path “/pages”:

<http://httpd.apache.org/docs/2.4/mod/mod_alias.html>

> or the mod_rewrite rule might look like
> RewriteRule "^/portfolio" "/page.php?name=portfolio"

This will *rewrite* *everything* starting with “portfolio” *and* continue;
for it to be equivalent to the above, it has to be

RewriteRule "^/portfolio" "/page.php?name=portfolio" [redirect,last]

But firstly, not redirecting is better in this case because then the URI
remains type-agnostic (so you should remove “redirect” and keep “last”), and
secondly the problem described above remains. Something like

RewriteRule "^/portfolio/?$" "/page.php?name=portfolio&%{QUERY_STRING}"
[noescape,last]

could work (watch for word-wrap).

--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

apoorv....@gmail.com

unread,
Jul 22, 2015, 9:10:22 AM7/22/15
to
Guys I am sorry if I did not put my question correctly I know we can do that with htaccess but I have to manually enter redirect code in the htaccess.

I want it to be dynamic so that if user enter title of the page contact us the url in the browser will be example.com/contact us.

kind of like wordpress where it does not have multiple pages but one default template and url and title of all the pages are different.

can we do that with $_SERVER['REQUEST_URI']

Gordon Burditt

unread,
Jul 22, 2015, 6:34:27 PM7/22/15
to
> Guys I am sorry if I did not put my question correctly I know we
> can do that with htaccess but I have to manually enter redirect
> code in the htaccess.

1. Put in a directive manually to redirect an entire directory (and
everything under it) to a PHP script. Redirecting the entire
site is a little tricky (where do you put the script, which you
DO NOT want to redirect?).

If you can figure out how to do it, you might try to redirect
everything EXCEPT URLs begining with /page .

2. Write the PHP script so it uses $_SERVER['REQUEST_URI'] to look
up the tail end of the URI (with variables taken off), in a
database, and use the resulting information to generate a page.
If there is no information on the page entered, generate an
error page.


> I want it to be dynamic so that if user enter title of the page
> contact us the url in the browser will be example.com/contact us.

This information needs to go in a database - what some user entered
somewhere in the past won't be remembered otherwise.

> kind of like wordpress where it does not have multiple pages but
> one default template and url and title of all the pages are different.

> can we do that with $_SERVER['REQUEST_URI']

That's one part of the solution.

Christoph M. Becker

unread,
Jul 22, 2015, 7:12:26 PM7/22/15
to
apoorv....@gmail.com wrote:

> Guys I am sorry if I did not put my question correctly I know we can
> do that with htaccess but I have to manually enter redirect code in
> the htaccess.
>
> I want it to be dynamic so that if user enter title of the page
> contact us the url in the browser will be example.com/contact us.
>
> kind of like wordpress where it does not have multiple pages but one
> default template and url and title of all the pages are different.
>
> can we do that with $_SERVER['REQUEST_URI']

I suggest you have a look at the CGI variable `PATH_INFO`, which is
supposed to be available in PHP via `$_SERVER['PATH_INFO']`.

--
Christoph M. Becker


Denis McMahon

unread,
Jul 22, 2015, 7:47:11 PM7/22/15
to
On Wed, 22 Jul 2015 06:10:05 -0700, apoorv.kanungo wrote:

> Guys I am sorry if I did not put my question correctly I know we can do
> that with htaccess but I have to manually enter redirect code in the
> htaccess.
>
> I want it to be dynamic so that if user enter title of the page contact
> us the url in the browser will be example.com/contact us.
>
> kind of like wordpress where it does not have multiple pages but one
> default template and url and title of all the pages are different.
>
> can we do that with $_SERVER['REQUEST_URI']

I thought I answered this already!

You set up your htaccess so that everything goes to a php page.

In the php page, you look at the server request variable and use that to
calculate which content to deliver.

The url shown in the browser address bar will be the url that it
requested. If you wish to change this, you need to use a redirect to the
url that wish to have displayed. You can do this in the same php page.

In apache, at the host level, use:

AliasMatch ^/.* /index.php

Then every request will get passed to /index.php.

In /index.php, you can look at any request data (post or get) as well as
the request uri, and then decide to do one of the following:

(a) Display content relevant to the request uri; or
(b) Issue a redirect header from the php to change the request uri; or
(c) Display default content

So then a request for http:/blah.blah.blah/blah?id=15 can trigger a
redirect to http:/blah.blah.blah/documents (because your php knows that
id 15 is documents), and http:/blah.blah.blah/documents gets processed
according to the request uri and the documents content is delivered to
the visitors browser.

Note that whether you do the redirections and processing in apache or php,
there is a need to first of all create and then manage mappings of (a) id
to content name and (b) content name to content source.

--
Denis McMahon, denismf...@gmail.com

Thomas 'PointedEars' Lahn

unread,
Jul 24, 2015, 2:31:43 AM7/24/15
to
Denis McMahon wrote:

> In apache, at the host level, use:
>
> AliasMatch ^/.* /index.php
>
> Then every request will get passed to /index.php.

To a *file* named “index.php” located *in the root directory
of the filesystem*. _Not_ in the Web root. A file that
most certainly does not and in any case SHOULD NOT exist.
A file that in a default configuration *for good reasons*
cannot be accessed:

---------------------------------------------------------
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
---------------------------------------------------------

RTFM.
0 new messages