Forum Moderators: phranque

Message Too Old, No Replies

Reformatting the URL

Changing what the user sees in the URL bar

         

calvinmicklefinger

4:51 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



I have the following in my .htaccess file

# Allow Properly formatted URL to access content
RewriteRule ^([a-z0-9]{5})/([a-z0-9]*)/?$ ./php/content.php [NC,L]

# Correct Improperly formatted URL and allow access to content
ReWriteRule ^([a-z0-9]+)/?$ 10000/$1 [NC]
RewriteCond %{REQUEST_URI} ^10000/([A-Za-z0-9]+)$
RewriteRule (.*) ./php/content.php [NC,L]

I would like the improperly formatted request to show the numeric "10000" in the URL bar in the users browser. This does not achieve that goal. Is it possible?

Thanks.

jdMorgan

6:05 pm on Jan 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course; just change the syntax from an internal rewrite to an external redirect. An internal rewrite is a file substitution inside the server, while an external redirect actually communicates with the client browser, telling it to start a new HTTP request with the server-provided (new) URL.

So


RewriteRule (.*) /php/content.php [NC,L]

becomes

RewriteRule (.*) [b]http:[i][/i]//www.example.com[/b]/php/content.php [NC,[b]R=301[/b],L]

(I left the leading dot off your substitution because I'm not sure why you included it. You may have to tweak this code as a result of that).

Jim

calvinmicklefinger

1:43 am on Jan 26, 2005 (gmt 0)

10+ Year Member



Tried the example with cut and paste, plus several other variations, and each time I received an Internal server error.

To recap, my .htaccess contains ...

# Allow Properly formatted URL to access content
RewriteRule ^([a-z0-9]{5})/([a-z0-9]*)/?$ ./php/content.php [NC,L]

# Correct Improperly formatted URL and allow access to content
ReWriteRule ^([a-z0-9]+)/?$ 10000/$1 [NC]
RewriteCond %{REQUEST_URI} ^10000/([A-Za-z0-9]+)$
RewriteRule (.*) ./php/content.php [NC,L]

While this sequence flows smoothly, I would like it when my visitor types [mydomain.com...] that it appear in the URL as [mydomain...] when the page is loaded, however it stays the same.

I would also like to maintain session info. In prior testing, using [mydomain.com......] started a new session.

Also, it appears that on my server the leading dot on the redirect target is necessary as the starting point (representing the current diretory) to direct the path up to the correct directory and the document.

Any other suggestions?

Many thanks.

calvinmicklefinger

9:09 pm on Jan 26, 2005 (gmt 0)

10+ Year Member



So it takes me awhile to understand. Now I have this ...

# Allow Certain Perl Scripts to run
RewriteRule ^cgi-bin/y/d.cgi/?([a-z]+)-IPP([0-9]+)$ - [NC,L]

# Allow Properly formatted URL to access content
RewriteRule ^([a-z0-9]{5})/([a-z0-9]+)/?$ ./php/content.php [NC,L]

# Allow Improperly formatted URL to access content and assign default PERL_SCRIPT_NUM
ReWriteRule ^([a-z0-9]+)/?$ [mydomain.com...] [NC,R=301,L]

It dawned on me that the $_SESSION variable I was declaring and using was set on the first page the visitor arrived at, and since there would be no more redirects (only rewrites), the variable would survive.

Thanks for your help.

PS - Don't know what was happening to cause the Internal Server Error when I used the redirect before. Maybe something in the rule?

jdMorgan

11:47 pm on Jan 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, an external redirect ends the current HTTP transaction, and the browser will then start a new one with the URL supplied in the 301/2 redirect response. So you could indeed get a new session, depending on how your session-creation works.

As for the 500-Server Error, I don't know. You can always put the old (bad) code back on the server and test it, then check the server error log to see what problem it caused. ;)

Jim