redirect the htaccess with 2 parameters - .htaccess

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.

Related

use ? in first of url

I want use below link and load from category.php but it does not work
http://test.com/?search=x
.htaccess:
RewriteRule ^search$ category.php?search=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test\.com$
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ http://category.php?search=%1 [R=302,L]
The reason you can't get away with a single RewriteRule is that query parameters cannot be directly rewritten. Instead, the above code captures the query string using RewriteCond %{QUERY_STRING}, which is then made available as %1 in the RewriteRule which follows. The RewriteCond %{REQUEST_URI} ensures that the code will only evaluate for the test.com domain.
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ category.php?search=%1 [QSA,NC,L]
You can use this simple rule:
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^/?$ category.php [L]
Pattern ^/?$ ensures that we match only landing page, nothing else.
QUERY_STRING is automatically appended to target URI so there is no need to capture and append in target URI.

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]

Remove parts of the URL mod_rewrite

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]

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

.htaccess mod_rewrite cannot remove page name

Having trouble figuring out the mod rewrite for .htaccess I want the url http://www.example.com/archive.php?title=about_me which is a dynamic url to be rewritten to http://www.example.com/about_me. I am using php and here is my current .htaccess code, however it only rewrites to http://www.example.com/archive/about_me want the archive to be removed.
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /archive/%1? [L,R=301]
RewriteRule ^/?archive/(.*)$ /archive?title=$1 [L]
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
I did get it to rewrite correctly with this code
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteRule ^/(.*)$ /archive?title=$1 [L]
However it then returns a page cannot be found error
I you want the /archive/ to be removed, you'll have to ensure that any URI that's in the form of /something must absolutely be routed to the archive.php script. Because there's simply no way to tell whether /my_blog is actually a request for /my_blog or whether it needs to be sent to the archive.php script with "my_blog" as the value of title in the query string. The best you can do is check that it's not a request for an existing resource via the -f and -d conditions:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/archive\.php\?title=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# no /archive/ ^
# condition checks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /archive?title=$1 [L]
Something like this should do the trick:
RewriteCond %{QUERY_STRING} ^title=(.*)
RewriteRule ^archive.php /%1?
RewriteRule ^(.*)$ /archive?title=$1 [L]
EDIT: Added the final RewriteRule as noted in my comments on the original question. Per the comment I believe you are trying to do the following two things:
Redirect any user-entered "real" URLs to the "friendly" URL: http://www.example.com/archive.php?title=about_me to http://www.example.com/about_me as stated in the question.
Rewrite the "friendly" URL to the "real" URL: http://www.example.com/about_me to http://www.example.com/archive.php?title=about_me, which was not clear as stated.

Resources