Forum Moderators: phranque

Message Too Old, No Replies

url rewrite with get variables

shortening the url, and keeping get variable in the url

         

rmario

7:49 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



So I've been searching for what seems to be ages. I need help with the following. I've searched and searched but don't get seem to find what i want

I was able to make my URL get trimmed from this:
www.domain.com/index.php?page=1 to this:
www.domain.com/index/ using .htaccess.

But what has happened is now I can't pull my row from database because the id in the URL has been pulled from the URL using the .htaccess.

What can be done? Can someone help me out or at least provide me with the code to trim everything after .php
but that the variable in the URL isn't lost?

Thanks.

g1smd

8:15 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The URL request will need to contain all the information needed to let the server know what content is required to be served.

So, would you be better off with URLs like
www.example.com/1
here?

There's certainly no need for 'index' to appear in the URL either.

RewriteRule ^([0-9]+)$ /index.php?page=$1 [L]


Link to the new format
www.example.com/1
style URLs from the pages of your site.

rmario

8:28 pm on Apr 7, 2010 (gmt 0)

10+ Year Member



g1smd,

I added
"RewriteRule ^([0-9]+)$ /index.php?page=$1 [L]"
to my htaccess document, but it seems it did nothing. I have the following on the beginning of my .htaccess file:
Options +FollowSymLinks
RewriteEngine On


Am I doing something wrong?
Now another question is it possible to hide the number 1 to avoid tampering with the id field in my database? Or will I have to protect that another way?

rmario

1:10 am on Apr 8, 2010 (gmt 0)

10+ Year Member



g1smd,

Thanks so much, I was able to make it work!
Never mind the questions before, I want to know if it's possible instead of it being example.com/1
to it being
example.com/about/
so it having a string instead of an integer, and adding the ending slash

jdMorgan

1:25 am on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's possible to rewrite example.com/about to /index.php?page=about -- simply change the regular-expressions pattern to match a character group or to match the specific "page" names that you want to accept.

However, if you wish to rewrite example.com/about to /index.php?page=1, then that will have to be done on a page-by-page basis, because mod_rewrite has no other way to know that "about" should be mapped to "page=1".

Jim

rmario

1:50 am on Apr 8, 2010 (gmt 0)

10+ Year Member



thanks for the response jdMorgan,
You mentioned to change the regular expressions pattern to match a character group. Being that I'm super new to this I hope you bare with me, but in the example g1smd gave : RewriteRule ^([0-9]+)$ /index.php?page=$1 [L]
where would that go exactly?

Now you also mentioned that I can rewrite example.com/about to index.php?page=1 would a redirect work?

jdMorgan

2:16 am on Apr 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



?1) Please see the resources cited in our Forum Charter [webmasterworld.com]... They will be most helpful to you. We would be doing you no favor to just 'hand you' code that you do not understand, and cannot troubleshoot or maintain over time.

?2) You probably do not need (or want) an external URL-to-URL client redirect. This kind of situation almost always calls for an internal URL-to-filepath rewrite.

Jim

rmario

3:18 am on Apr 8, 2010 (gmt 0)

10+ Year Member



Thanks! A lot for giving me what I needed.

famouswebteam

8:57 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Hi, I found this thread in a search and it is similar to what I need to do, but can't get it working. I am launching a new site and need to redirect old urls to the new ones.

I am trying to redirect a lot of urls (a few thousand) like this:

http://www.example.com/news.cfm?id=1
http://www.example.com/news.cfm?id=2
http://www.example.com/news.cfm?id=3

to urls like:

http://www.example.com/News/General/Senate-snuffs-campus-smoking-ban
http://www.example.com/News/General/Playground-smoking-ban-fails-in-Upper-St-Clair
http://www.example.com/News/Cigars/Alec-Bradley-Family-Blend-cigars-expands-line-to-five-sizes

At first I tried this, but I learned from this thread that redirect doesn't handle url variables

redirect 301 /news.cfm?id=1 http://www.example.com/News/General/Senate-snuffs-campus-smoking-ban

It worked for other urls w/o get variables, but not these.

So then I tried to modify the suggested code from the above posts to fit my site, but with no luck.

RewriteRule ^([0-9]+)$ /news.cfm?id=$1 [L]


Any help? Thanks!

g1smd

12:29 am on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The last code is a rewrite, not a redirect.

In any case you can't use a redirect in .htaccess to fix your latest problem. You need to rewrite your old requests to a script. That script will need to look up the new URL by retrieving a record in the database, and then sending the appropriate 301 redirect headers.

famouswebteam

1:53 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



Thanks for your post. Any guidance/examples/tutorials on doing that?

Need to redirect from:
http://www.example.com/news.cfm?id=1

to:
http://www.example.com/News/General/Senate-snuffs-campus-smoking-ban

g1smd

5:59 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to rewrite your old requests to a script. That script will need to look up the new URL by retrieving a record in the database, and then sending the appropriate 301 redirect headers.

That's way out of scope for a forum posting. It's a serious amount of work.

famouswebteam

6:35 pm on Apr 12, 2010 (gmt 0)

10+ Year Member



You need to rewrite your old requests to a script. That script will need to look up the new URL by retrieving a record in the database, and then sending the appropriate 301 redirect headers.


Actually, I am fine with most of that. I can easily write a script that will look up the id in the database and send them to the new url with 301 headers. I already have a table setup with "oldurl" and "newurl", so I know exactly where everything needs to redirect to.

The only part I need help with is rewriting the old requests. If I get the filename and id passed to my script, thats all I would need to redirect them to the appropriate page.

So in "http://www.example.com/news.cfm?id=3", how can I rewrite/redirect the url to go to that new script and pass it "news.cfm" and "id=3"?

jdMorgan

6:48 pm on Apr 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check for "news.cfm" in the RewriteRule and "id=" in a RewriteCond examining %{QUERY_STRING}, and if they match, internally rewrite the request to your script's filepath. You can either back-reference these matched URL-path-parts and pass them to your script as name-value pairs appended to its filepath, or you can just invoke the script let the script itself get them from the server request variables.

Jim

famouswebteam

7:13 pm on Apr 12, 2010 (gmt 0)

10+ Year Member



Check for "news.cfm" in the RewriteRule and "id=" in a RewriteCond examining %{QUERY_STRING}, and if they match, internally rewrite the request to your script's filepath. You can either back-reference these matched URL-path-parts and pass them to your script as name-value pairs appended to its filepath, or you can just invoke the script let the script itself get them from the server request variables.

Jim


Thanks for your response Jim.

Can you show an example of how to check for news.cfm in the rewriterule and id= in a rewrite condition? I am not very familiar with using htaccess.

Thanks.

jdMorgan

2:19 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Our Apache Forum Charter and Apache Forum Library may prove useful. Links to both are at the top of this page.

Please post your best-effort attempt at coding a solution as a basis for discussion of specific questions, so that in the end you will understand your code and be able to maintain it.

If the nature of this reply is unclear, please review our Forum Charter.

Thanks,
Jim

famouswebteam

3:52 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



FYI for anyone doing something simlar...I figured it out.

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^news\.cfm$ /redirect.php?page=news [QSA,L]

redirect /news.cfm http://example.com/redirect.php

And thats it. Then in my script, I looked for the url vars and redirected them to the appropriate page with 301 headers.

famouswebteam

5:09 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



Edit: didnt need the redirect line in there, just

RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^news\.cfm$ /redirect.php?page=news [QSA,L]

was all that was needed.

g1smd

5:28 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Several flaws.

The redirect should be listed before the rewrite.

The redirect should be changed to use RewriteRule with [R=301,L] flags.

As currently coded it is possible for internal filepaths to be exposed out on to the web as URLs.

famouswebteam

6:43 pm on Apr 13, 2010 (gmt 0)

10+ Year Member



well I removed the redirect line completely, and the new url's that I am redirecting to are already SEF urls, not actual file paths.

jdMorgan

7:22 pm on Apr 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are not redirecting (a URL-to-URL translation), you are rewriting (A URL-to-filepath translation). And that is why this thread got so confused.

You might consider adding a 301 redirect from the non-SEF URLs back to the SEF URLs, if those non-SEF URLs contain all of the information needed to do this. Doing so will prevent duplicate-content -- the same page accessible at more than one URL.

Be sure to check %{THE_REQUEST} using a RewriteCond. This will be necessary to prevent an 'infinite' rewrite-redirect loop in conjunction with your existing rewrite.

Jim