Forum Moderators: phranque

Message Too Old, No Replies

Internal redirect, how to?

How to rewrite a URL without changing the address bar

         

MrHaan

9:08 pm on May 1, 2009 (gmt 0)

10+ Year Member



Hi,
I'm a bit of a newbie here (and at Apache and English)
I want when visitors type like example.com/go/1
the web server loads the page example.com/go/index.php?ID=1
without changing the adressbar, like a internal rewrite (or something)
I use those "go-links" in my applications like permalinks and i don't want to make like 100 directories:
example.com/go/1
example.com/go/2
example.com/go/3
etc...

example.com/go/1 looks much better than example.com/go/index.php?ID=1 (it's a small url... whit bigger URL you will see better)

I don't really know how to explain (don't know the words in english)

How do I do that in apache? htaccess? CGI?

MrHaan

Srry for bad English

jdMorgan

9:10 pm on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Start here: Changing Dynamic URLs to Static URLs [webmasterworld.com]

Jim

MrHaan

9:34 pm on May 1, 2009 (gmt 0)

10+ Year Member



I read it and I get an HTTP 5000 error Internal Server Error

this is my .htaccess in my root directory
Options -Indexes
# Enable mod_rewrite, start rewrite engine
Options +FollowSymLinks
RewriteEngine on
#
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^go/([^/]+)/?$ go/index.php?ID=$1 [L]
#
# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{2,9}\ /index\.php\?ID=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://example.com/go/%1? [R=301,L]

"go is the subdir where the script is"

MrHaan

Edit: I seems I have It working again...(only wrong para meter passed to the script)
I get index.php when I "echo" PHP global variable $_GET["ID"] not the number...

<snip>

[edited by: MrHaan at 9:47 pm (utc) on May 1, 2009]

jdMorgan

10:12 pm on May 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your output filepath can match your pattern, so your rule creates an 'infinite' rewriting loop. Add an exception so that index.php is not rewritten to itself:

# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteCond $1 !^index\.php$
RewriteRule ^go/([^/]+)/?$ go/index.php?ID=$1 [L]

Jim

MrHaan

9:57 am on May 2, 2009 (gmt 0)

10+ Year Member



Yeah Tnx... It works....

but when I enter mydomain.com/go/index.php?ID=1, there is no 301 Permanent move to mydomain.com/go/1 or something...)

I finally begin to understand what the code does xD

MrHaan

jdMorgan

1:51 pm on May 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you've posted here does not do a redirect from go/index.php?ID=1 to /go/1
The code here captures client HTTP requests for the URL-path /go/1 and sends them to your script at filepath /go/index.php, moving the "1" from the URL into the "ID=" query string. This is an internal URL-to-filepath translation, termed an "internal rewrite."

In order to redirect direct client requests for URL example.com/go/index.php?ID=1 to URL example.com/go/1, you will need to add another rule, also discussed in the thread I cited above (described as optional step 3). This new rule will capture a client request for the dynamic URL and redirect it to the static URL. This is a URL-to-URL translation, termed an "external redirect," and involves sending an HTTP response to the client that says, "The resource that you have requested has moved. Please ask for it again at this new URL."

This may be clearer if you understand that mod_rewrite works as a client's HTTP request arrives at your server, and translates the client's incoming URL request to either an internal filepath (rewrite) or to another URL (redirect). This takes place before any content is served or any scripts are invoked.

Jim