Using Htaccess to Remove Part Of The URL - .htaccess

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!

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]

Related

redirect the htaccess with 2 parameters

I have a url with 2 parameters that I want to redirect the user to a clean url
https://www.example.com/songs?date=1999&btn-login=
I need it to be
https://www.example.com/songs/1999
how can I do this?
I have tried this but the url remain as it is without any changes
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /songs\.php\?date=(.*)&btn-login=\ HTTP
RewriteRule ^ /%2? [R,L]
#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /songs.php?date=$1 [L]
Try
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(|.+&)date=([^&]+) [NC]
RewriteRule ^songs$ /songs/%2? [R=301,L]
%2 is used to read from the 2nd group which should be the full value of the date param in the query string. ? is used so the query string isn't appended in the redirect. I also made it so that the regex won't allow something like anotherdate=.
You can view the regex in detail here.

Redirect to anothe page using .htaccess

I want to redirect the url of http://sitename/modules/fb/fb.php to http://sitename/modules/take_control .How can i do that using htaccess
I tried like
RewriteRule ^/?modules/fb/fb\.php$ take_control , Is that right ?
Your solution is the opposite of what you most likely want if you are trying to rewrite to take_control.
Give this a try. These rules should provide the rewrite with take_control as the URI both ways.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+modules/fb/fb\.php [NC]
RewriteRule ^ /take_control/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^take_control/?$ /modules/fb/fb.php [L]

RewriteEngine Adds relative path

I have such rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^monkey.pl(.*) [NC]
RewriteRule ^(.*)$ http://www.monkey.pl/$1 [R=301,L]
RewriteRule ^horse.html$ /dog.html
and when I go to the monkey.pl/horse.html I get the message:
The requested URL /home/login/monkey/dog.html was not found on this server.
How can I get this to work. Basically what I'm trying to do is to change address of urls like:
http://www.monkey.pl/produkty.php?strona=1
to be displayed as
http://www.monkey.pl/produkty/czesci_do_mixokretow.html
but none of my rules are working. Therefore I'm trying to come with solution.
I tried many varations and I couldn't get it to work. I don't want to rewrite whole page. Just 6 pages which I need to change url and that's all. Fixed translation url => url.
If you are only doing a handful of URLs then you can do them this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} /+produkty\.php\?strona=1 [NC]
RewriteRule ^ /produkty/czesci_do_mixo‌​kretow\.html [R=302,L]
RewriteRule ^produkty/czesci_do_mixo‌​kretow\.html$ /produkty.php?strona=1 [L]
RewriteRule ^horse\.html$ /dog.html [L]

Removing part of URL but don't redirect with htaccess

So the problem I'm looking at and haven't managed to solve!
For example the url is http://someurl.com/brands/brand
What I want to accomplish is that htaccess lets te request go to url: http://someurl.com/brands/brand but removes the "brands" part from the url.
Also if the url: http://someurl.com/brands is called the page "brands" needs to be displayed. So only when there is a brand after /brands/ it needs to do a URL rewrite to http://someurl.com/brand
I have tried this but this piece does a redirect to the new location witch doesn't exist.
RewriteRule ^onze-merken/(.*)$ /$1 [L,R=301]
So I need the above with out the redirect, it only needs to rewrite the URL.
Any help would be greatly appreciated, Thanks!
This is now my htaccess part where the rewriting is done!
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /onze-merken/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# Only rewrite if the directory doesn't exist.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# internal forward from pretty URL to actual one
RewriteRule ^((?!onze-merken/).+)$ /onze-merken/$1 [L,NC]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteRule ^(uploads/([a-z0-9_\-\.]+)/__images/custom/(.*)/)(.*)$ http://someurl.com/uploads/$2/__images/custom/$3/$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /brands/(\S+)\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!brands/).+)$ brands/$1 [L,NC]

How do I rewrite a single parameter using htaccess?

I have URLs in the following format:
http://www.mysite.com/index.php?l=accommodation/lodge/some-place
I would like to use htaccess to rewrite it to this format:
http://www.mysite.com/accommodation/lodge/some-place
How would I do that? I started trying to do it with drupal's htaccess file and got something like this:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?l=$1 [L,QSA]
Problem is, if I enter the original index.php url, it still shows. It must redirect to the short version. In other words, this:
http://www.mysite.com/index.php?l=accommodation/lodge/some-place
Must forward to this:
http://www.mysite.com/accommodation/lodge/some-place
Thoughts?
Insert this additional rule above your rule:
RewriteCond %{THE_REQUEST} \s/+index\.php\?l=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
This will externally redirect http://www.mysite.com/index.php?l=accommodation/lodge/some-place to http://www.mysite.com/accommodation/lodge/some-place

Resources