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
  •  | 
  •  

Pregunta

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

¿Fue útil?

Solución

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

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top