-2

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]
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Prudhvi Sree
  • 1
  • 1
  • 1
  • It's useful to explain exactly what is "not working" when asking a question. "Not working" is not particularly helpful. – MrWhite Aug 11 '15 at 11:59

2 Answers2

1

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;
    }
    ?>
Sebastian
  • 11
  • 2
0

[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
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • tanq w3d. but still the url is not changing. actually i am trying in xampp(localhost) so is there any problem..? – Prudhvi Sree Aug 12 '15 at 12:47
  • Initially i get 500 internal server error, now that error not coming after edited the code as u mentioned but the desired url is not coming.(actually no change in my original url) – Prudhvi Sree Aug 12 '15 at 12:58
  • Please state in your question the exact URL you are requesting and the desired outcome. – MrWhite Aug 12 '15 at 14:16
  • my actual url is "property-details.php?pid=PID1" and the desired url is "property-details.php/PID1" – Prudhvi Sree Aug 13 '15 at 05:03
  • I assume you mean `property-details/PID1` ie. no `.php` in the desired URL? And the code in your question suggests your _actual URL_ is `/real/property-details.php?pid=PID1` - in a `/real` subdirectory - is that not the case? I've updated my answer. – MrWhite Aug 13 '15 at 06:42
  • yes your are correct my desired url is property-details/PID1 . "real" is my project name in htdocs. My project path is xampp->htdocs->real->(here php pages). ".htaccess file is along with my php pages". – Prudhvi Sree Aug 13 '15 at 07:44
  • here is my updated code Options +FollowSymLinks RewriteEngine On RewriteBase /reall/ RewriteRule ^property-details/([a-zA-Z0-9]+) property-details.php?pid=$1 [NC,L] still not working – Prudhvi Sree Aug 13 '15 at 07:48
  • Your code looks correct, however, the .htaccess file should be located in your document root, not in the subdirectory. – MrWhite Aug 13 '15 at 07:52
  • you mean the .htaccess file path xampp->htdocs-> (here) ? i tried this also with the same above code but still desired url not came. – Prudhvi Sree Aug 13 '15 at 08:10