I need help with PHP .htaccess rewrites

Discussion in 'PHP' started by Glen Huff, Nov 27, 2015.

  1. #1
    I have a url in the following syntax:
    website.com/gallery/model/site/album/image

    The main file handling the variables is gallery.php. The categories following gallery in the url are to be the variables: $model $site $album $image in php. I want the php extension removed, and the 4 variables routed to the gallery page. This has been giving me a lot of trouble. What I have so far in my .htaccess file is:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]


    RewriteEngine On
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^gallery/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ gallery.php?model=$1&site=$2&gallery=$3&image=$4 [NC,L]



    The code above is from tutorials and me trying to implement them. I'm getting a 500 internal server error. I'm not sure how to put this rewrite together. Can I get some help? Thank you for those who took the time to read this.
     
    Glen Huff, Nov 27, 2015 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    Dear Glen,

    I tried it, although am not good at REGEX but I have a small solution for you that may help.
    Create index.php that will tackle all as per number of arguments.

    Thus, your .htaccess
    
    Options +FollowSymLinks
    RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^gallery/((?s).*)$ index.php?_url=/$1 [QSA,L]
    
    Code (markup):
    Your index.php
    
    <?php
    
        $url = $_GET['_url'];
        $url = substr($url,1, strlen($url));
    
        list($data['model'],$data['site'],$data['album'],$data['image'])
                = preg_split('/\//', $url);
    
        print_r($data);
    
    Code (markup):
    then process the associative array called $data your way.

    I hope it helps, let me know if you required more help.

    stay well...
     
    Vooler, Nov 30, 2015 IP