Hi, it is possible change this URL with .htaccess?
From
To ( seo-friendly without ‘news’ ): aaaa-bbb-ccccc

Dani AI

Generated

A few practical points and a short checklist to complement and : the thread already shows two viable approaches — keep a namespaced path like /news/slug (safer) or try root-level slugs (/slug) which will intercept many other site paths. covered the redirect+internal-rewrite flow and the redirect-loop pitfall; gave a compact pattern that is useful as a starter but only fits a very specific slug shape. For a robust, SEO-friendly migration the strategy should cover canonicalization, legacy redirects, exclusions, testing and monitoring.

  • Decide a canonical pattern first (include or omit a trailing slash, force lowercase). Consistency is the single biggest SEO win.
  • Internally rewrite the canonical path to news.php?slug=… while excluding real files and directories and disabling MultiViews so content-negotiation does not interfere.
  • Perform a server-side redirect from the old query-string URLs to the canonical path using a 301 once testing is complete; during testing use temporary redirects to avoid cached 301s that mask mistakes.
  • Update internal links, the XML sitemap, and the canonical link element on the page. Submit the sitemap to Search Console and monitor for crawl errors.
  • Prefer placing rewrite rules in the virtual host config rather than .htaccess when possible (better performance). For nginx the same logic belongs in the server block.

Troubleshooting and edge cases: normalize slugs in the application (lowercase, transliterate or percent-encode non-ASCII), watch for collisions if mapping at root (assets, admin pages), and check redirect headers with curl -I to confirm status codes and Location values. Increase rewrite logging on the server if a loop or unexpected match appears. Following these steps preserves link equity and avoids accidental routing of unrelated URLs to the news handler.

Recommended Answers

All 5 Replies

Just to understand what you are asking. Do you want when somebody visits to redirected automatically through .htaccess rules to with 301 Moved Permanently status code , and when you have a visit to to have your server treat it through .htaccess rules as if were ?

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 = + 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 to ?

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 to and treat as if it were .

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 to and treat as if it were . (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.