Can I use HTACCESS to format this URL?

Viewed 49

I would like to know if it is possible to format a URL like https://site1.com/en/article-details.test-article?item=test to https://site1.com/en/article-details/test-article?item=test so basically changing the .test-article to /test-article.

Because of how my site works it only recognizes .test-article as a valid selector so reading up https://httpd.apache.org/docs/2.4/rewrite/flags.html it seems I may need to use a PT flag to show the user the desired URL but pass the correct one to my site.

So far I have this to replace the dot for a slash

RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([a-z]{2})(/?)$
RewriteRule ^/([a-z]{2})/.*  https://%{SERVER_NAME}/$1/$2 [L,NC,R=301]

Edit(this works to format the URL as per RavinderSingh13's suggestion)

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([a-z]{2})/article-details.([^?]*)(/?)$
RewriteRule ^ https://%{SERVER_NAME}/%1/article-details/%2 [QSA,R=301,NE,L]
2 Answers

With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.(test-article)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]

As per OP's comments adding more Generic rules here:

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(en/article-details)\.([^?]*)\?item=test\s [NC]
RewriteRule ^ https://site1.com/%1/%2 [QSA,R=301,NE,L]

You may try this rule as your topmost rule:

RewriteEngine On

RewriteRule ^(.+)\.(test-article/?)$ /$1/$2 [R=302,NE,L,NC]

Change 302 to 301 after testing this rule.

Related