Is there a way via htaccess to change URLs to clean URLs by deleting a parat of an URL?

StackOverflow https://stackoverflow.com/questions/22739157

  •  23-06-2023
  •  | 
  •  

Question

My all URLs formats are like these:

http://example.com/category?format=feed
http://example.com/category/sub_category/?format=feed

for example: http://example.com/computer/cpu/?format=feed

Is there a way via htaccess to change top URLs to these:

http://example.com/category/feed
http://example.com/category/sub_category/feed

example1: http://example.com/computer/cpu/feed

example2: http://example.com/computer/feed

Was it helpful?

Solution

These rules should meet both requirements. But allows you to change the category and subcat as long as the normal URLs don't change and format is always a key in the query string.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/(.+)$ /$1/$2/?format=$3 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1/?format=$2 [L]

Then you should be able to use these URL types.

http://example.com/category/feed
http://example.com/category/sub_category/feed

OTHER TIPS

The following should achieve what you are looking for:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)/(.*)/$ /$1/$2/%1? [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)$ /$1/%1? [L,R]

The above will turn:

http://example.com/category?format=feed to http://example.com/category/feed

and

http://example.com/category/sub_category/?format=feed to http://example.com/category/sub_category/feed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top