Remove parts of the URL mod_rewrite - .htaccess

I have this URLs:
mysite.com/index.php?id=que-ofrecemos
mysite.com/index.php?id=quienes-somos
And I want to make it look like this:
mysite.com/que-ofrecemos
mysite.com/quienes-somos
In fact, I want to remove index.php?id= only from URLs who have id set.

If you want your URL's to look like this in the browser
mysite.com/que-ofrecemos
mysite.com/quienes-somos
Then you need to use a rule like this. This will also redirect any URL with the id query string back to this mysite.com/quienes-somos. Try these rules.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php?id=([^&\ ]+)
RewriteRule .* /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [NC,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]

.htaccess rewrite pretty url

I'm new to .htaccess coding and I'm finding it way more confusing than html and css. I want to make my url's more seo friendly and just easier for my users. My current code isn't working. I want to make my urls look like my example below. Let me know if it's possible, I'd prefer not to show my id numbers.
example url
http://mobile.mixtapemonkey.com/artist?a=96/2-chainz
solutions I can live with
.com/2-chainz
.com/a/2-chainz
.com/artist/2-chainz
RewriteCond %{THE_REQUEST} \s/+artist\?a=([^&]+)&t=([^\s&]+) [NC]
RewriteRule ^ a/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)/([^/]+)/?$ artist.php?a=$1&t=$2 [L,QSA,NC]
If you accept the rewrites with the ID in URL, try the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /artist\?a=(\d+)/(\S*) [NC]
RewriteRule ^ a/%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^a/(\d+)/(.*)/?$ artist.php?a=$1/$2 [L,QSA,NC]
It'll rewrite the URL: http://mobile.mixtapemonkey.com/artist?a=96/2-chainz to: http://mobile.mixtapemonkey.com/a/96/2-chainz.

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!
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]

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