Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

htaccess - remove middle part of url from a &_GET


I have urls like this:

www.example.com/artworks/arte-1/english-name-1

I want to remove arte-1 and make the urls like this:

www.example.com/artworks/english-name-1

Both arte-1 and english-name-1 come from &_GET results. Sometimes they are the same, like

www.example.com/artworks/dimension/dimension

But I want to get the first and third parts of the url. And even if they are different, can't find a way to do it. One of the things I tried:

RewriteRule ^artworks/([a-z0-9-]+)/?([a-z0-9-]+)$ $1 [R=302,L]

And I also tried:

RewriteCond %{THE_REQUEST} artworks/([a-z0-9-]+)/([a-z0-9-]+)
RewriteRule ^ artworks/$2? [L]
RewriteCond %{THE_REQUEST} artes/([a-z0-9-]+)/([a-z0-9-]+)
RewriteRule ^ artes/%2? [L]

I placed them right after RewriteEngine On, but it doesn't work. My working htaccess is:

RewriteEngine On

RewriteRule ^artworks\/([a-z0-9-]+)\/([a-z0-9-]+)\/?$ index.php?id=artworks&pintura=$1&versao=$2 [L]
RewriteRule ^artes\/([a-z0-9-]+)\/([a-z0-9-]+)\/?$ index.php?id=artes&pintura=$1&versao=$2 [L]

RewriteRule ^([a-z0-9-]+)\/?$ index.php?id=$1 [L]

Can i please get some help?


Solution

  • With your shown attempts, please try following rules. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##Newly added rules here.
    RewriteCond %{THE_REQUEST} \s/(artworks)/(?:[\w-]+)/([\w-]+)\s [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    RewriteRule ^(artworks)/([\w-]+)/?$ index.php?id=$1&versao=$2 [NC,L]
    
    ##OP's already existing rules edited a bit for improvement here.
    RewriteRule ^artworks/([\w-]+)/([\w-]+)/?$ index.php?id=artworks&pintura=$1&versao=$2 [NC,L]
    RewriteRule ^artes/([\w-]+)/([\w-]+)/?$ index.php?id=artes&pintura=$1&versao=$2 [NC,L]
    RewriteRule ^([\w-]+)/?$ index.php?id=$1 [L]