Sgtmullet
Sgtmullet

Reputation: 335

Using Htaccess to Remove Part Of The URL

I need to remove the /design/ from this URL (and any that contain it).

https://www.domain.com/design/subcategory/productname1

At the end, I want it to look like this:

https://www.domain.com/subcategory/productname1

But since that would obvious cause a 404 of you just deleted that part of the URL, I need to populate that second URL with the contents of the first. I apologize if I am not using the correct terms but here is an example of something similar I have on my site.

In my header menu, I have a link that indicates it will take you here:

https://www.domain.com/index.php?route=product/search&tag=shirts

But I wanted it to look like this:

https://www.domain.com/shirts/

So I researched it and this solution worked great:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]

Can something similar be done to remove the /design/ part of the URL?

Thanks!

Upvotes: 1

Views: 799

Answers (1)

anubhava
anubhava

Reputation: 785146

Try these rules:

RewriteEngine On

# remove /design/ from URLs and redirect
RewriteCond %{THE_REQUEST} \s/+design/(\S*)\s [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# internally add design/ to know set of URIs
RewriteRule ^(skulls-bones|characters|abstract|animals|games|geek-nerd|graphic|keep-calm|movies-tv|pop-culture|tattoo|typography)(/.*)?$ design/$1$2 [L,NC]

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+product/search/\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/design/ [NC]
RewriteRule ^([^/]+)/?$ product/search/?tag=$1 [L,QSA]

Upvotes: 1

Related Questions