skip to Main Content

I’m trying to generate seo friendly urls using a htaccess file and PHP. But it’s not working.

I tried the code below in localhost (running XAMPP)

RewriteEngine On
RewriteBase /real/

RewriteRule ^property-details/([0-9a-zA-Z]+) property-details.php?pid=$1 [NC, L]

2

Answers


  1. [NC, L]

    You need to remove the space between your RewriteRule flags. This will result in a 500 Internal Server error – in which case you should examine your error logs for a more meaningful error. It should be written as:

    [NC,L]
    

    No spaces.


    UPDATE:
    For mod_rewrite to work in per-directory .htaccess files, FollowSymLinks must also be enabled (if its not already). At the very top of your .htaccess file, include:

    Options +FollowSymLinks
    
    Login or Signup to reply.
  2. There are different ways of achieving the same results, but this will give you a guide.

    First activate mod_rewrite uncommenting this line in you httpd.conf or apache2.conf file:

    LoadModule rewrite_module libexec/apache2/mod_rewrite.so
    

    Change AllowOverride from none to All to your www directory

        <Directory />
            AllowOverride All 
            Require all denied
        </Directory>
    

    Restart apache:

    sudo apachectl restart
    

    or

        sudo service apache2 restart
    

    In your www folder, create an .htaccess file and add this:

        # | SEO URL                                                                                
        Options +FollowSymLinks  
        RewriteEngine On  
        RewriteCond %{SCRIPT_FILENAME} !-d  
        RewriteCond %{SCRIPT_FILENAME} !-f  
        RewriteRule ^.*$ ./index.php
    

    Then in your index.php file:

    <?php
        $url_params=get_url_params();
    
        // then access your url_params
        if (isset($url_params[1]))
        {
            switch ($url_params[1]) 
            {
                case 'login':
                        echo "<h1>Login</h2>";
                    break;
    
                case 'contact':
                        echo "<h1>Cantact</h2>";
                    break;
    
                default:
                    echo "<h1>Home</h2>";
                    break;
            }
    
        }
    
        function get_url_params($site_url='')
        {
            $base_url=explode("/", $site_url);
            $request  = $_SERVER['REQUEST_URI'];
            $url_params = explode("/", $request);
            $delete_extensions=array('.html','.htm');
            $data[]=array();
            foreach ($base_url as $b)
            {
                unset( $url_params[array_search( "$b", $url_params )] );
            }
            foreach ($url_params as $u)
            {
                foreach ($delete_extensions as $e){
                    $u=str_replace($e, "", $u);
                }
                $data[]=$u;
            }
            return $data;
        }
        ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search