Search code examples
php.htaccessxampp

How to make seo url with htaccess on localhost?


i have liste.php file, I call the php file like this

127.0.0.1/test/liste.php?id=yeni

liste.php file is located at site.com/test folder.

How can I make this SEO friendly as follows?

127.0.0.1/test/liste/yeni

I'm running the site localhost via xampp.

thank you


Solution

  • This should be

    SORRY I forgot the /test in the target :-( (it is no good idea to answer regex questions while watching TV)

    Put the following into your httpd.conf

    UPDATE:

    RewriteEngine On 
    RewriteRule ^/test/(\w*)/(\w*)/?$  /test/$1.php?id=$2 [NC,L]
    

    This will do the following: http://test/liste/yeni/

    It will take the text between /test/ and the next slash / in this case liste and use it as the name of the php file

    and take the text after the slash (with or without trailing slash) in this cas yeni as id

    If it should only rewrite to /test/liste.php?id=xyz so oly the id is variable and not the name of the php file, then it would be even easier.

    AND DO NOT FORGET TO RESTART APACHE after changing the httpd.conf

    IF this DOES NOT WORK then switch on tracing add the line LogLevel alert rewrite:trace3 before your rewrite rule.

    So you have now the following lines at the end of your httpd.conf

    RewriteEngine On 
    LogLevel alert rewrite:trace3
    RewriteRule ^/test/(\w*)/(\w*)/?$  /test/$1.php?id=$2 [NC,L]
    

    RESTART APACHE!

    and go with your browser to to http://test/liste/yeni/

    Now look into the error log located e.g. at C:\xampp\apache\logs

    Now you can see all single steps of the mod_rewrite from your original clean URL to the "real url" and can see if it works or at wich stage failure happens.

    [Tue Jan 05 22:58:37.283228 2021] [rewrite:trace2] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - - [127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] init rewrite engine with requested uri /test/liste/yeni/
    
    [Tue Jan 05 22:58:37.284227 2021] [rewrite:trace3] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - - [127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] applying pattern '^/test/(\\w*)/(\\w*)/?$' to uri '/test/liste/yeni/'
    
    [Tue Jan 05 22:58:37.284227 2021] [rewrite:trace2] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - - [127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] rewrite '/test/liste/yeni/' -> '/test/liste.php?id=yeni'
    
    [Tue Jan 05 22:58:37.284227 2021] [rewrite:trace3] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - - [127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] split uri=/test/liste.php?id=yeni -> uri=/test/liste.php, args=id=yeni
    
    [Tue Jan 05 22:58:37.284227 2021] [rewrite:trace2] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - -[127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] local path result: /test/liste.php
    
    [Tue Jan 05 22:58:37.284227 2021] [rewrite:trace2] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - - [127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] prefixed with document_root to C:/xampp/htdocs/test/liste.php
    
    [Tue Jan 05 22:58:37.284227 2021] [rewrite:trace1] [pid 6724:tid 1828] mod_rewrite.c(483): [client 127.0.0.1:54029] 127.0.0.1 - -    [127.0.0.1/sid#2a4d7e8a298][rid#2a4d8998330/initial] go-ahead with C:/xampp/htdocs/test/liste.php [OK]
    

    IT worked :-) Voilá

    In access.log you will get only one line but with the correct code 200 :-)

    127.0.0.1 - - [05/Jan/2021:22:58:37 +0100] "GET /test/liste/yeni/ HTTP/1.1" 200 154 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    

    If its working again remove the tracing line or put a # on the beginning of the line to mark it as comment

    Reference: https://httpd.apache.org/docs/2.4/rewrite/intro.html

    https://httpd.apache.org/docs/2.4/rewrite/flags.html

    Try your regexps here and learn interactively: (you have to escape the / with a backslash ) https://regex101.com/