Rewrite search page query URL with htaccess - regex

I want when search form submitted, the URL be like this :
localhost/xampp/external/proj3/search/searchquery
Instead of this:
localhost/xampp/external/proj3/tools/search/search.php?query=searchquery

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /xampp/external/proj3
# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?query=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]
# internal forward from pretty URL to actual URL
RewriteRule ^search/([^/.]+)/?$ search.php?query=$1 [L,QSA,NC]

Create a .htaccess file in your document root. And put this:
RewriteEngine On
RewriteRule ^search/([a-zA-Z0-9]+)/?$ \
search.php?query=$1 \
[NC,L]
Note: Be careful with the regular expression. Filter it cautiously.

Related

Fix the regex code in .htaccess for beautifying this url

my current regex code to beautify a url (www.abc.com/user.php?url="rio") in .htaccess file is
RewriteCond %{REQUEST_URI} user
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ user.php?url=$2
RewriteRule ^([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)/$ user.php?url=$2
It works fine and the page url is beautified to :
www.abc.com/user/rio
but there is a problem now, Now even if I change url like in following ways:
www.abc.com/user/user/user/rio
www.abc.com/user///rio
www.abc.com/user/abc/rio
These links work same way, and that is making content duplication.
Please help!
You may try these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# remove multiple slashes
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
# handles /user/something
RewriteRule ^user/([\w-]+)/?$ user.php?url=$1 [L,QSA,NC]
Options -MultiViews is required to turn off content negotiation feature.

.htaccess redirect URL with id param to human friendly URL

I'm trying to create a URL redirect but so far everything I have tried just hasn't shown any effect on the site. I know ModRewrite is enabled as there are other rewrites taking place. The whole purpose of this is to handle old URLs from the former version of the website.
What I want to achieve is a redirect of a URL with the following format:
/resources/view?id={id} and redirect it to /resources/{id}.
I've been trying to do so with variants of this:
RewriteRule ^resources/view?id=([0-9+])$ /resources/$1 [R=301,L]
and also this:
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^/resources/view$ /resources/$1? [R=301,L]
Cheers.
You can use these 2 rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /resources/view\?id=(\d+) [NC]
RewriteRule ^ /resources/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^resources/(\d+)/?$ resources/view?id=$1 [L,QSA,NC]

RewriteRule when there is ?id=

I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on

How to rewrite this URL using .htaccess

I am basically just trying to write a .htaccess redirect that turns the url
www.example.com/Artist/Profile?Artist='test' to www.example.com/Artist/test
without actualy redirecting the user.
use the following directive
RewriteCond %{QUERY_STRING} (^|&)artist='([^&]+)' [NC]
RewriteRule ^Artist/Profile$ /Artist/%2? [L,NC]

Htaccess Querystring rewrite

I have URLs like
/?test that I want to rewrite to /page.php?page=test
I tried like this but for some reason it doesn't work.
RewriteRule ^\?([a-z0-9\-\+]{3,20})/?$ /page.php?page=$1 [NC,QSA]
What am I doing wrong?
The query string can only be tested with the RewriteCond directive. The RewriteRule pattern is only tested against the URL path (in .htaccess files the URL path without the per-directory prefix).
So try this:
RewriteCond %{QUERY_STRING} ^[a-z0-9-+]{3,20}$ [NC]
RewriteRule ^$ /page.php?page=%0 [QSA]