Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts

    How to set mod_rewrite so that the url was not changed in the address bar

    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.

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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...40&postcount=4
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #3
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    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!


    Quote Originally Posted by ShaneC View Post
    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...40&postcount=4
    Last edited by camaross; 01-22-2011 at 03:06 AM.

  • #4
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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]
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #5
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    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!


    Quote Originally Posted by ShaneC View Post
    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]

  • #6
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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]
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #7
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Just tried. The error is the same: 404.

    I appreciate the continuous help very much.



    Quote Originally Posted by ShaneC View Post
    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]
    Last edited by camaross; 01-22-2011 at 03:33 AM.

  • #8
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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 (/).
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #9
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Now it works, but the resulting url has been changed to:
    Code:
    http://www.mysite.com/search.php?search=var1
    again.

    Yes, search.php is in my root directory.


    Quote Originally Posted by ShaneC View Post
    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 (/).

  • #10
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    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:

    Code:
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #11
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    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.

    Quote Originally Posted by ShaneC View Post
    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:

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

  • #12
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    I've never actually done this myself but seen it a few times in passing. Try something like this:

    Code:
    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.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • Users who have thanked ShaneC for this post:

    camaross (01-22-2011)

  • #13
    Regular Coder
    Join Date
    Jan 2011
    Posts
    136
    Thanks
    101
    Thanked 0 Times in 0 Posts
    Many thanks.

    I really appreciate your help very much!!!

    Quote Originally Posted by ShaneC View Post
    I've never actually done this myself but seen it a few times in passing. Try something like this:

    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •