homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

mod rewrite
how should the url look before

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

The way I have done my mod-rewrites is to name the pages after the name, e.g.

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



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

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 ;)


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

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?


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

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.

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

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved