Welcome to WebmasterWorld Guest from 54.147.44.93

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

mod rewrite

how should the url look before

     

whatson

3:46 am on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The way I have done my mod-rewrites is to name the pages after the name, e.g.
example.com/page.php?name=test-page

Then in the mod rewrite I tell it to change to example.com/test-page

But is there a way to make example.com/page.php?id=1 to become example.com/test-page

lucy24

5:33 am on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Yes, but not by asking mod_rewrite to read your mind.

If there are only a few, do it manually. If there are lots of them, mod_rewrite will only do the first part of the job: rewrite (not redirect) to a php page that looks up "1" and finds "test-page" -- or the other way around -- and then redirects to the page you want. The first step is a Rewrite so you don't come out with two consecutive Redirects. Search engines do not like that.

Or, ahem, since we are already in the php forum-- did you mean, exactly what will your php page look like? Oops. Can't help you there.

I am assuming for the sake of discussion that you know what "rewrite" and "redirect" mean, and you're just saying "become" as a shortcut ;)

whatson

9:30 pm on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks lucy, yes correct assumption.

So I create a php look up page, that says if page id=1, then url=name?

Then how is this written in the htaccess?

Is the way I am doing it, acceptable also?

g1smd

11:01 pm on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



So I create a php look up page, that says if page id=1, then url=name?

No. Other way round. When user requests
example.com/page-name
the request is rewritten (that's rewritten not redirected) to
/page-generator.php
as
/page-generator.php?name=page-name
which looks up which record number that page name matches (sends 404 header and error message if no match) and then fetches the content from the database, assembles the page and sends it out.

Use extensionless URLs and a specific RegEx pattern that accepts only extensionless requests so that your system doesn't try to rewrite requests for robots.txt or for css and js files etc.

For the URLs, I prefer to include the ID number at the beginning (like
/1-page-name
or similar), making it easier to fetch the content, and to allow the page name to be changed without changing the ID. Before the content is fetched, the php script does a look up to ensure the requested page name is correct for the requested page number. If they don't match, it sends a redirect to the correct URL.

lucy24

11:15 pm on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Overlapping g1, so expect some duplications and/or contradictions

Depends on exactly what's in the "wrong" URL that you want to redirect. For example:

RewriteCond %{QUERY_STRING} id=\d+
RewriteRule page\.php /fixup.php [L]

By default, Rewrites keep the existing query string, so you don't need to capture or do anything about it. Some people are afraid of \w and \d so you can say [0-9]+ if you prefer. Your fixup page will then look up the pretty name that goes with "id={number}" and issue a 301 Redirect to the page.

And then presumably you've got a second php page that takes the pretty URL and finds the location that holds its content, using another Rewrite. If this second Rewrite again uses "id={number}" then your first RewriteRule will need to include a line saying something like

RewriteCond %{THE_REQUEST} id=[0-9]

so you don't go around in circles.

Is the way I am doing it, acceptable also?

Probably, but I can't say, since you don't show your actual RewriteRule :) But if it works, you are probably doing it right.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month