Forum Moderators: phranque

Message Too Old, No Replies

rewrite and mask the url

how to rewrite a URL and show the original URL only..

         

mktulasi

10:43 am on Aug 3, 2010 (gmt 0)

10+ Year Member



Hi,
I have [abc.com...]
I want to redirect the request comes to [abc.com...] and it should be redirected to [abc.com...] , But it should show only [abc.com...] in the status bar.

I am using IBM HTTPserver
and in virtual host i made
RewriteEngine on
RewriteRule /def [%{SERVER_NAME}...] [L]

its redwriting, but showing in the status bar [abc.com...]
where i need only to show [abc.com...]

Please help me asap..

jdMorgan

1:11 pm on Aug 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code is not rewriting, it is redirecting the client. This is because you specified a redirect with the syntax of your code.

A redirect is a URL-to-URL translation. On executing your rewriterule, the server sends a response to the client that says, "The resource you requested has moved. Please ask for it again at the following URL. It then provides the URL that you specified in your rule. This ends the current HTTP transaction, and the client must start a new one -- although this is optional, and the client may decide not to do so, or to check with the user before doing so.

A rewrite, by contrast, is a URL-to-filepath translation. It simply modifies the server-internal filepath associated with the requested URL. It says to the server, "If you get a request for this URL, serve the contents (or invoke the script) at that filepath." Here, the server simply substitutes a different filepath than the one that it would normally use by default. This is done entirely within the context of the original client HTTP request, and the client is unaware of the URL-to-filepath re-mapping.

So the important points here are not to confuse URLs with filepaths, and not to confuse external client URL redirects with internal server filepath rewrites. URLs are used outside the server -- "out on the Web." Filepaths are used inside the server, and should never be visible on the Web. They are not at all the same thing, and filepaths are "associated" with URLs only by the action of a server.

Having cleared up the terminology and the basic concepts, we can address the specific problem with your code. I cannot guarantee that this will work exactly as written, because IBM HTTPserver is not Apache, but I assume that the syntax is 'close' if not exactly the same...

RewriteEngine on
#
# Internally rewrite requests for URL example.com/def to filepath /xyz.html
RewriteRule ^/def$ /xyz.html [L]

Again, I'm not sure about exact compatibility with Apache, but if your code is located in either a .htaccess file or inside a <Directory> container in a server config file, then you may need to remove the leading slash from the RewriteRule pattern, making it just "^def$", as is the case on Apache. If the code is not located inside a <Directory> container in a server config file, then the leading slash will be required in order to match the requested URL-path.

On Apache, a redirect is specified by doing one of two things. If you specify either a full protocol+URL in the RewriteRule's substitution field or add an [R=30x] flag to the rule, then a client redirect is invoked. If indeed you intend to invoke a redirect, then both should be specified to avoid problems on servers where the configured ServerName is different from your preferred canonical hostname (e.g. non-www versus www hostnames) and where the UseCanonicalName option is set to "on." On shared hosting where Webmasters cannot correct the configured ServerName, this prevents redirects to the non-canonical hostname.

Jim

Crump

2:35 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Is there a way to feed the directory in to a PHP script?

For instance if I have a website with many users, and I want them to be able to access their page like this:

http://www.mywebsite.com/crump

but the output is really coming from this script:

http://www.mywebsite.com/processuser.php?whichuser=crump

but I want it to appear to be coming from the original directory.

Any help with this? Trying to search the net but I don't really know where to start!

jdMorgan

3:14 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is trivial, and has been covered many times here. However, finding the right words to search for isn't easy.

# Internally rewrite requests for URL-path /users/<username>
# to filepath /processuser.php?whichuser=<username>
RewriteRule ^/users/([a-z_\-]+)$ /processuser.php?whichuser=$1 [L]

Note that I put the "users" into a virtual subdirectory called "users" and I made the username pattern accept only letters, underscores, or hyphens. This is advisable to prevent problems with "collisions" between usernames and your real files and subdirectories. For example, if this is not done, then a user signing up as "stats" or "robots.txt" or "images" might well take your server down or ruin your search rankings overnight...

You can name the "users" subdirectory anything you like -- "/users", "/myuserspages", or just "/u" -- but it should be a unique and separate "URL-partition."

Also don't confuse URLs and URL-paths with filenames and filepaths. They are different things. Files are associated with URLs only by the action of the server, and need not have anything at all in common. The server will use a default 'mapping' rule (remove protocol and domain, prepend DocumentRoot) or you can modify this mapping by using mod_rewrite. But a URL is not a file and a file is not a URL. They are completely-different "naming methods" -- URLs for use "out there on the Web" and filepaths for use "here inside this server's filesystem.

In fact, the main reason for the existence and use of URLs is so that Web clients (such as browsers) won't have to know anything about your server's operating system, hard drive name, and directory structure...

Jim

Crump

4:43 am on Aug 8, 2010 (gmt 0)

10+ Year Member



Jim:
Thank you for your help, I've got it partially working. One thing I can't seem to get is how to get my PHP variables from the URL also follow the redirect.

In other words, my script is a calendar script

When I type in the url:

<domain.com>/user/anyuser

it redirects and passes the anyuser to a script that is located at:

<domain.com>/user/script.php

However when the user tries to change months, it does not work. I end up getting:

<domain.com>/user/anyuser?m=7&y=2010

But PHP doesn't see those variables. I've tried researching the net for the last hour but can't seem to figure it out. Your help is very much appreciated.

Crump

5:14 am on Aug 8, 2010 (gmt 0)

10+ Year Member



I seemed to have finally found a website that helped me out and gave me some good guidance on a PHP function:

[mattlowden.com...]

It uses mb_parse_str

It works, but is it the best way to do what I need to do?

jdMorgan

11:28 pm on Aug 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Internally rewrite requests for URL-path /users/<username>, and pass original query strings
# to filepath /processuser.php?whichuser=<username>
RewriteRule ^/users/([a-z_\-]+)$ /processuser.php?whichuser=$1 [QSA,L]

Jim