Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule and php code

         

xsamy

8:48 pm on Nov 9, 2009 (gmt 0)

10+ Year Member



Hello guys. How to set a rewrite rule and PHP code for the following:

A User requests a page like test.com/samy.john. But this page does not exist, so he will get redirected to /index.html and in the headline of index.html there will be a message that says Hello Samy which is taken from the first part of the URL
he requested.
the rewrite rule for redirect to index.html is done and working as following :
rewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.html [R,L]

but how to display the 1st name on the index.html page after redirect anyone can help me with code
thanks

Receptional Andy

8:53 pm on Nov 9, 2009 (gmt 0)



Welcome to WebmasterWorld, xsamy :)

To do this you will need to set a cookie to store the user information, unless you are prepared to pass the parameters in the URL itself.

To use the cookie method, internally rewrite the URL to a script which can set a cookie, and then issue the redirect via that script.

xsamy

7:13 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



Hello Andy ,
Well im not very well in coding that why i need some one help me with coding
here with i did
htaccess as
rewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /index.html [R,L]

so any request like test.com/xsamy.john
will be redirect to test.com/index.html
index.html is normal html page
the part i cant do as code is when user redirect from page he request like test.com/andy.samy
he got welcome msg at index.html
say Hello $1stnameof url like Hello Andy
this my goal :)

Receptional Andy

8:36 pm on Nov 10, 2009 (gmt 0)



If you redirect someone from /xsamy.john to /index.html, there is no way for a script to know that the original request was for /xsamy.john. The only way to preserve the name is to store it an cookie. You can write cookies with PHP scripts, but not with redirects in an htaccess file.

So, my suggestion is that you do not redirect with htaccess. Instead you rewrite to a PHP script which will be able to set the cookie and perform the redirect.

In htaccess, something like

RewriteRule ^(.*)$ /index.php?name=$1 [L]

Then, in index.php store the username using a cookie [google.com] and then output a redirect [google.com].