...

View Full Version : How to set mod_rewrite so that the url was not changed in the address bar



camaross
01-22-2011, 01:34 AM
I want to change the url in the background and execute the php program, but do not want to change the appearance of the original url. Is this possible?

for example,

If I input www.mysite.com/var1, I want it to be converted to www.mysite.com/search.php?search=var1, and execute the php program, but I also want to see the original url (www.mysite.com/var1) after the php program.
Is there a way I can set this, or I will have to convert it back?

Thank you very much.

ShaneC
01-22-2011, 01:54 AM
This is actually how mod_rewrite operates by default. Essentially what it does is reads http://mysite.com/var1 and then the server reads that as http://mysite.com/search.php?search=var1. So long as all your links use the Clean URL format the user will never see the unclean URLs.

Take a look at this mini-guide I posted in response to another user's question for information on how to use mod_rewrite, as well as a very useful mod_rewrite tutorial:

http://www.codingforums.com/showpost.php?p=1032840&postcount=4

camaross
01-22-2011, 03:02 AM
Thank you so much for the help.

Below is my rule: RewriteRule ^([a-zA-Z0-9]+)$ http://www.mysite.com/search.php?search=$1 [L, R=301]

Unlike the default, it will change the url. I guess it might be due to the [L, R=301]. If I delete the [L, R=301], I will get error for my website (404 Not Found: The resource requested could not be found on this server!).

Any suggestions will be greatly appreciated!



This is actually how mod_rewrite operates by default. Essentially what it does is reads http://mysite.com/var1 and then the server reads that as http://mysite.com/search.php?search=var1. So long as all your links use the Clean URL format the user will never see the unclean URLs.

Take a look at this mini-guide I posted in response to another user's question for information on how to use mod_rewrite, as well as a very useful mod_rewrite tutorial:

http://www.codingforums.com/showpost.php?p=1032840&postcount=4

ShaneC
01-22-2011, 03:04 AM
Try removing the R=301 flag, that marks the rewrite as a redirect. If this generates an error please post the specific error.

RewriteRule ^([a-zA-Z0-9]+)$ http://www.mysite.com/search.php?search=$1 [L]

camaross
01-22-2011, 03:08 AM
Thank you, Sir.

I just tried this. The error is the same (firefox):

404 Not Found
The resource requested could not be found on this server!



Try removing the R=301 flag, that marks the rewrite as a redirect. If this generates an error please post the specific error.

RewriteRule ^([a-zA-Z0-9]+)$ http://www.mysite.com/search.php?search=$1 [L]

ShaneC
01-22-2011, 03:11 AM
Well there's actually a few things here, now that I take another look, that are concerning me. Try this (I realize that it isn't the same as the Regular Expression you specified, but for debugging sake):

RewriteRule ^(.*)$ search.php?search=$1 [L]

camaross
01-22-2011, 03:15 AM
Just tried. The error is the same: 404.

I appreciate the continuous help very much.




Well there's actually a few things here, now that I take another look, that are concerning me. Try this (I realize that it isn't the same as the Regular Expression you specified, but for debugging sake):

RewriteRule ^(.*)$ search.php?search=$1 [L]

ShaneC
01-22-2011, 03:22 AM
Try this instead:

RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

I've tested it and it works for me. Keep in mind search.php must be in the root of your web directory (/).

camaross
01-22-2011, 03:26 AM
Now it works, but the resulting url has been changed to:

http://www.mysite.com/search.php?search=var1 again.

Yes, search.php is in my root directory.



Try this instead:

RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

I've tested it and it works for me. Keep in mind search.php must be in the root of your web directory (/).

ShaneC
01-22-2011, 03:28 AM
Check your .htaccess file for conflicts and make sure there is no redirection happening in search.php. Also ensure that an forms being submitted are being submitted to the clean URL and not the complex one.

This is the entire htaccess file of my test:



RewriteEngine On
RewriteBase /

RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

camaross
01-22-2011, 03:33 AM
Man, you are a genius. Now I found the problem. I had the below rule in front of the one we are working on. After I delete it. Everything is working flawlessly.

RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301]

Thank you so much for helping me out. BTW, do you know how I can still redirect my other domain names to my site without the above rule?

Thank you thank you.


Check your .htaccess file for conflicts and make sure there is no redirection happening in search.php. Also ensure that an forms being submitted are being submitted to the clean URL and not the complex one.

This is the entire htaccess file of my test:



RewriteEngine On
RewriteBase /

RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

ShaneC
01-22-2011, 03:41 AM
I've never actually done this myself but seen it a few times in passing. Try something like this:



RewriteCond %{HTTP_HOST} ^www.myRedirectDomain.com$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]


Where myRedirectDomain is the domain you want to redirect to mysite.com.

Place this above the rewrite rule we just created.

camaross
01-22-2011, 03:46 AM
Many thanks.

I really appreciate your help very much!!!


I've never actually done this myself but seen it a few times in passing. Try something like this:



RewriteCond %{HTTP_HOST} ^www.myRedirectDomain.com$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]


Where myRedirectDomain is the domain you want to redirect to mysite.com.

Place this above the rewrite rule we just created.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum