Hi, it is possible change this URL with .htaccess?
From https://www.example.it/news.php?slug=aaaa-bbb-ccccc
To ( seo-friendly without ‘news’ ): https://www.example.it/ aaaa-bbb-ccccc

Just to understand what you are asking. Do you want when somebody visits https://www.example.it/news.php?slug=aaaa-bbb-ccccc to redirected automatically through .htaccess rules to https://www.example.it/aaaa-bbb-ccccc with 301 Moved Permanently status code , and when you have a visit to https://www.example.it/aaaa-bbb-ccccc to have your server treat it through .htaccess rules as if were https://www.example.it/news.php?slug=aaaa-bbb-ccccc ?

If this is the case , don't you serve anything else in your domain ? If you make this change everything under your domain will be treated if as if were https://www.example.it/news.php?slug= + anything . If you serve only this in your domain I could give you more details of how to do it but wouldn't it be more logical to map https://www.example.it/news.php?slug=aaaa-bbb-ccccc to https://www.example.it/news/aaaa-bbb-ccccc ?

commented: I also have category and publisher http://localhost/example/category.php?slug=lex but they all contain news +1

Your .htaccess file would look something like:

RewriteEngine On

RewriteRule ^([a-z]+)-([a-z]+)-([a-z]+)$ news.php?slug=$1-$2-$3 [L]

This is untested, but it should work.

commented: As jkon Thanks In localhost it gives me URL: http://localhost/example/news/aaaa-bbb-ccccc And The requested URL was not found on this server. +1

First case: redirect https://www.example.it/news.php?slug=aaaa-bbb-ccccc to https://www.example.it/news/aaaa-bbb-ccccc and treat https://www.example.it/news/aaaa-bbb-ccccc as if it were https://www.example.it/news.php?slug=aaaa-bbb-ccccc .

RewriteEngine On

RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /news\.php\?slug=([^&\ ]+)
RewriteRule ^ /news/%2? [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?news/([^/]+)/?$ %{ENV:BASE}/news.php?slug=$1 [L]

Second case: redirect https://www.example.it/news.php?slug=aaaa-bbb-ccccc to https://www.example.it/aaaa-bbb-ccccc and treat https://www.example.it/aaaa-bbb-ccccc as if it were https://www.example.it/news.php?slug=aaaa-bbb-ccccc . (but of course with that everything that is under your domain will be send to news.php)

RewriteEngine On

RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /news\.php\?slug=([^&\ ]+)
RewriteRule ^ /%2? [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ %{ENV:BASE}/news.php?slug=$1 [L]

I have made several assumptions here (e.g. here I assume that your slug variable doesn't have slash) but I believe is generic enough to work.

commented: Thanks In localhost it gives me URL: http://localhost/example/news/aaaa-bbb-ccccc And The requested URL was not found on this server. +0

Hello Adolfo_1 . It took me a while because I didn't saw your response (if it was a reply and not a comment I would have seen it to activity stream). Lets suppose that your .htaccess file is directly under the example folder and your news.php is also there. Feel free to remove the comments (lines starting with # especially in production)

#Getting the relative path to your project base URL to %{ENV:BASE}
RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]
RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]

#First rule
#If the original requested URL before any redirects happened was /news.php?slug= and anything not & , / , space . This is neccesarry because if we don't put this condition we will have a redirect loop in the next rule (from /news/anything to /news.php?slug=anything)
RewriteCond %{THE_REQUEST} /news\.php\?slug=([^&\/\ ]+) [NC]
#if there is a QUERY_STRING that starts with slug= and the variable doesn't have & , / , space then it will use this variable as %1
RewriteCond %{QUERY_STRING} ^slug=([^&\/\ ]+)$
#Then if the requested file is news.php then redirect it with 301 to news/anything where anything is the slug variable 
RewriteRule "^news\.php$" %{ENV:BASE}news/%1? [R=302,L]


#Second Rule 
#If the request is /news/anything redirect it internally to /news.php?slug=anything
RewriteRule ^/?news/([^/]+)/?$ %{ENV:BASE}news.php?slug=$1 [L]

I also use nginx in newer servers but also maintain some with apache (most of my company new apps have the client / visitor app in Java and the Admin in PHP so nginx is great for that). In those I use a simple rule .. redirect internally everything that is not in the file system in a front controller of the framework (not even the app).

That was an interesting question because of the redirect loop and I learned more answering it about the %{THE_REQUEST} apache variable in conditions. Reviewing this answer some days after writing it I realized an apparent error. As I tested it in a real apache server I had to make the response not 301 but 302 in order not having to clear by browser cache in every attempt. So obviously it is:
RewriteRule "^news\.php$" %{ENV:BASE}news/%1? [R=301,L]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.