Results 1 to 6 of 6
  1. #1

    Newbie 301 .htaccess regular expression help

    What I have now...

    mydomain.com/myscript.php?category=fish&type=tuna
    I would like all links to end with &page=1 like the link below...

    mydomain.com/myscript.php?category=fish&type=tuna&page=1
    So if anyone types into the address bar the first link without "&page=1", they get redirected to a link with the page number as you see in the second quote.

    Can anyone help or maybe point me in the right direction?

    Thanks guys

  2. #2
    Join Date
    May 2003
    Location
    USA
    Posts
    53
    Try something like:

    Code:
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} !(^|&)page=
    
    RewriteRule ^myscript.php$ $0?page=1 [QSA]
    You could alternatively do this in your PHP script:

    PHP Code:

    if ( ! isset( $_GET'page' ] ) ) {

      
    $_GET'page' ] = 1;

      
    $query_string http_build_query$_GET );
      
      
    $url "http://{$_SERVER'HTTP_HOST' ]}{$_SERVER'SCRIPT_NAME' ]}?{$query_string}";

      
    header"Location: {$url});
      
      exit;



    By the way, you should use example.com or one of its relatives in situations where you used mydomain.com here.

  3. #3
    Thanks JMM! You made my day!

    I couldn't get the htaccess code to work, but I got the PHP code working without any problem :-)

    I slightly edited the above code to add a 301 permanent redirect like below.

    PHP Code:
    header"Location: {$url}"TRUE301); 
    Thanks again!

  4. #4
    Join Date
    May 2003
    Location
    USA
    Posts
    53
    Cool, I'm glad it helped you.

    Quote Originally Posted by Topkat325 View Post
    I couldn't get the htaccess code to work
    Sorry, that was my bad. To accomplish what you want, you'd need an external redirect to the new URI, something like:

    Code:
    RewriteEngine On
    
    RewriteCond %{HTTPS} =on
    
    RewriteRule .* - [E=HTTPS_CHAR:s]
    
    RewriteCond %{QUERY_STRING} !(^|&)page=
    
    RewriteCond %{ENV:HTTPS_CHAR} (.*)
    
    RewriteRule ^myscript.php$ http%1://%{HTTP_HOST}/$0?page=1 [QSA,R=301,L]
    Note that the last RewriteCond is used because the Apache documentation does not document the ability to interpolate an environment variable of the form %{ENV:variable} in a RewriteRule substitution. I suspect it works and is supposed to work and the documentation is just bad. In the documentation for the RewriteRule E flag, it says:

    Environment variables can then be used in a variety of contexts, including...other RewriteRule directives...
    You can see my discussion of a similar issue under the heading @trowel's technique in this post.

    Quote Originally Posted by Topkat325 View Post
    I slightly edited the above code to add a 301 permanent redirect like below.
    Excellent.

    You're welcome.

  5. #5
    I just tried your .htaccess code and it works great. I wish I was as good as you with htaccess and PHP :-)

    Thanks again mate! I really appreciate your help. :-)

  6. #6
    Join Date
    May 2003
    Location
    USA
    Posts
    53
    Quote Originally Posted by Topkat325 View Post
    I just tried your .htaccess code and it works great. I wish I was as good as you with htaccess and PHP :-)

    Thanks again mate! I really appreciate your help. :-)
    Ok, great. Thanks for the kind feedback, I appreciate it.

Similar Threads

  1. regular expression help
    By larwilliams in forum Programming Discussion
    Replies: 5
    Last Post: 06-01-2009, 07:13 PM
  2. Regular Expression Help
    By smkied in forum Programming Discussion
    Replies: 1
    Last Post: 11-21-2008, 02:37 PM
  3. Using Regular Expression to...
    By NetVer in forum Programming Discussion
    Replies: 5
    Last Post: 12-30-2004, 08:16 AM
  4. Is my regular expression right?
    By eagleknight in forum Web Design and Content
    Replies: 9
    Last Post: 10-14-2004, 12:36 AM
  5. Regular Expression
    By raulgonzalez in forum Programming Discussion
    Replies: 2
    Last Post: 09-12-2003, 06:51 PM

Posting Permissions

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