skip to Main Content

I wanted to make seo-friendly URLs, but I have some problems while setting up the .htaccess file. First of all, I’ve made it possible to get rid of file extension (.php) and the link shows properly.
example.com/pond.php => example.com/pond
The problem begins when I try to add $_GET variables to the link.

What works for me is either example.com/pond.php?a=1&b=2 or example.com/pond?a=1&b=2.

What I would like to achieve is example.com/pond-1-2, but when I go to this link I get site not found

My .htacess file

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.*)$ $1.php [NC,L]
#extension removed

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

#RewriteRule ^pond/([0-9]+)/([0-9a-zA-Z_-]+) pond.php?id=$1&name=$2 [NC,L]
#RewriteRule ^pond-(.*)-(.*)$ pond.php?id=$1&name=$2 [NC,L]

RewriteRule ^pond-([^.]+)-([^.]+)$ pond.php?id=$1&name=$2 [NC,L]

The commented rules are what I’ve tried previously, but none of these work for me. I’ve tried solutions from many topics on stackoverflow, but these also do not work for me :(.

2

Answers


  1. Chosen as BEST ANSWER

    RewriteRule ^(.*)-(.*)$ $1.php?id=$2 [NC,L] This was the solution to my problem.


  2. I prepared 3 different examples. I hope it helps you or others.

    Version 1:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^pond([-]([^-]+))?([-]([^-]+))?([-]([^-]+))?$ pond.php?a=$2&b=$4&c=$6 [NC,L]
    

    Test


    Version 2:

    RewriteEngine On
    RewriteRule ^pond$ pond.php [L]
    RewriteRule ^pond-([^-]+)$ pond.php?a=$1 [L]
    RewriteRule ^pond-([^-]+)-([^-]+)$ pond.php?a=$1&b=$2 [L]
    RewriteRule ^pond-([^-]+)-([^-]+)-([^-]+)$ pond.php?a=$1&b=$2&c=$3 [L]
    

    Test


    Version 3:

    RewriteEngine On
    RewriteRule ^pond$ pond.php [L]
    RewriteRule ^pond/([A-Za-z0-9-]+)$ pond.php?a=$1 [L]
    RewriteRule ^pond/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ pond.php?a=$1&b=$2 [L]
    RewriteRule ^pond/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ pond.php?a=$1&b=$2&c=$3 [L]
    

    Test

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search