.htaccess question / url rewrite (?)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1395

    #1

    Tech .htaccess question / url rewrite (?)

    I know this should be possible but I'm having difficulty figuring out how.

    I have a php variable at the top of my page like this:

    $collection = "blah blah something something";
    What I'm trying to do is have it so that my .htaccess file will rewrite the URL of the page it is on to contain the verbiage inside that variable and that will become the url of the page like this:

    domain.com/directory/blah-blah-something-something.php

    Would anyone happen to know if this actually is doable, the page has a .php extension and secondly, would you happen to have the .htaccess code needed to make this happen?

    Thanks for any help you can offer.

    Oh and Happy Mardi Gras everyone

  • zijlstravideo
    Confirmed User
    • Sep 2013
    • 806

    #2
    Sure, that's doable.

    Your php file, it should look something like this:

    $collection = "blah blah something something";
    $actual_Link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $post_Link = preg_replace("![^a-z0-9]+!i", "-", strtolower($collection));
    $post_Link = rtrim($post_Link, "-");
    $rewrite_Link = "http://$_SERVER[HTTP_HOST]/directory/".$post_Link.".php";
    if($rewrite_Link !== $actual_Link)
    {
    header("Location: $rewrite_Link,TRUE,301);
    die();
    }


    And your htaccess something like this:

    RewriteRule directory/(.*)\.php$ yourrealfile.php?id=$1 [L]

    Not entirely sure about the htaccess part though. It looks a bit off. Anyway, it's untested, but I think it should point you in the right direction.


    And make sure you get the value inside $collection from somewhere safe. Might want to check and verify if it's a match in your SQL database or something, before redirecting.
    Contact: email

    Comment

    • Publisher Bucks
      Confirmed User
      • Oct 2018
      • 1395

      #3
      Originally posted by zijlstravideo
      Sure, that's doable.

      Your php file, it should look something like this:

      $collection = "blah blah something something";
      $actual_Link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      $post_Link = preg_replace("![^a-z0-9]+!i", "-", strtolower($collection));
      $post_Link = rtrim($post_Link, "-");
      $rewrite_Link = "http://$_SERVER[HTTP_HOST]/directory/".$post_Link.".php";
      if($rewrite_Link !== $actual_Link)
      {
      header("Location: $rewrite_Link,TRUE,301);
      die();
      }


      And your htaccess something like this:

      RewriteRule directory/(.*)\.php$ yourrealfile.php?id=$1 [L]

      Not entirely sure about the htaccess part though. It looks a bit off. Anyway, it's untested, but I think it should point you in the right direction.


      And make sure you get the value inside $collection from somewhere safe. Might want to check and verify if it's a match in your SQL database or something, before redirecting.
      Awesome, thanks, will have a play with this shortly

      Comment

      • u-Bob
        there's no $$$ in porn
        • Jul 2005
        • 33063

        #4
        .htaccess :
        Code:
        RewriteEngine On
        
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .+ %{DOCUMENT_ROOT}/whereveryouwannakeepthis/404.php [L]
        404.php:
        Code:
        <?php
        
        $uri = $_SERVER['REQUEST_URI'];
        
        $path = parse_url($uri, PHP_URL_PATH);
        
        $filename = basename($path);
        
        if($collection = substr($filename, 0, strpos($filename, ".php")) )
        { 
        $collection = str_replace("-"," ",$collection);
        print $collection;
        }
        else
        {
        http_response_code(404);
        exit;
        }
        
        ?>

        Comment

        Working...