Question mark in htaccess - .htaccess

I'm trying to make search results url look like Google.
site.com/search?q=blabla
Tried this
RewriteRule ^search\?q=(.*)$ index.php?q=$1
I used \ just before ? but it still doesn't work, the question mark makes the problem. What is the correct way to do that?

You just need this simple rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^search/?$ index.php [L,QSA,NC]
Query string ?q=foobar will be automatically carried over to index.php.

Try this instead, this works for me to replace:
example.com/search.php?q=hello
With:
example.com/search/hello/
Code is:
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^search/?q=([a-zA-Z0-9-]+)$ /$1.php?q=$2`

Related

Remove certain part of URL

I have a website where i have pages like:
domain.com/?p=settings
domain.com/?p=daily
And i am looking for rewrite that ?p= part, so it would be like
domain.com/settings
So far i have tried to add this to htaccess files:
RewriteRule ^([^\.]+)$ $1?p= [NC,L]
but it did not worked.
Also I have tried look from Google but could not find any.
I have tried other RewriteRule's but they did not work either.
RewriteRule does not include query string. It is available as a separate variable enter link description here
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.
So the following won't work.
RewriteRule ^([^.]+)$ $1?p= [NC,L]
You need something like
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/ %2?
Checkout Apache Mod ReWrite Wiki and scroll down to "Making the Query String Part of the Path"
You were close with your attempt, you need to use this in your .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /?p=$1 [L]
Make sure to clear your cache before testing this.

.htaccess rewrite rule to change path to query string? [duplicate]

Ok, im pretty new at this and I would really appreciate some help, thanks!
How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/?url=contact
All i want to do is remove the query string
/contact
Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all
This was my solution:
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
To a user on your site, they will see and navigate to this:
http://example.com/contact
But the real page would be something like this:
http://example.com/index.php?url=contact
This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html %1
(or whatever if it's not index.html, index.php, whatever)
You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule
Before: https://example.com/index.php?user=robert
RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]
After: https://example.com/user/robert

.htaccess rewrite URL with a question mark "?"

My aim is this URL:
component/users/?view=registration
To:
registration.html
the .htaccess is in the folder mysite of the website.
i tried this:
RewriteBase /mysite
RewriteRule ^component/users/?view=registration$ registration.html$ [R=301,L]
But i doesnt work...
When i try this:
RewriteRule ^component/users/_view=registration$ registration.html$ [R=301,L]
it works very well.
So how can i fix this problem with the question mark. I already read thats it is not a part of the URL (its appended). I have read that i have to use something like querystring, but i didn't really understand the syntax.
Maybe someone could write the solution of this problem? Would be awesome =)
You need to use %{QUERY_STRING} to capture the query string data:
RewriteCond %{QUERY_STRING} ^view=(.*)$
RewriteRule ^component/users/?$ %1.html? [R=301,L]
The above rule/condition will take the value of the query string view and use it to form your redirect if the path component/users matches.
RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]
You need a \ because ? is part of a regular expression. To Use it as a string u need to escape it with a \
Linktip: http://ole.michelsen.dk/tools/regex.html

Mod_Rewrite RewriteRule issue

I have a domain at example.com
There is a subdirectory that has a quiz on it, located at example.com/quiz/?id=1
I need to change the ?id=1 to TakeTheQuiz so it would look like example.com/quiz/TakeTheQuiz
Here is what my .htaccess looks like right now (the .htaccess is located in the root direct at example.com). Right now I always get a server 500 error.
RewriteEngine On
RewriteBase /quiz
RewriteRule ^?id=1$ TaketheQuiz
This is really simple and all of the examples I have seen have been really complicated and hard for me to apply it to this one :( Help, anyone? Thank you for your time.
You've just got the rule the wrong way round:
RewriteEngine On
RewriteBase /quiz
RewriteRule ^TaketheQuiz$ ?id=1 [L]
EDIT
Per your comment try this instead:
RewriteCond %{QUERY_STRING} id=1
RewriteRule ^$ TaketheQuiz [R=301,L]

.htaccess php to html

sorry for this simple question, however i still cant get my head round using .htaccess
I'm trying to convert:
search.php?s=dvd&page=1
to
/Search/dvd/page1.html
Thanks,
Jack
I think something like:
RewriteRule ^search/([A-Za-z]+)/page([0-9]+)\.html$ search.php?$1&page$2
Should do the trick.
Further reading here: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
you must put your link "/Search/dvd/page1.html" in the page and with htaccess it will convert to the "search.php?s=dvd&page=1" . i hope it be usefull :)
sample correct htaccess code :
RewriteEngine On
RewriteRule Search/(\d+)/page?(\d+)\.html$ search.php?s=$1&page=$2 [NC,L]
If I understand the question OP wants to convert php to html redirection. Try this rule in your .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# to match /search.php?s=dvd&page=1
RewriteCond %{QUERY_STRING} ^s=([^&]*)&page=(.*)$ [NC]
RewriteRule ^search\.php/?$ /Search/%1/page%2.html? [R,L,NC]
# to match /search.php?page=12&s=dvd
RewriteCond %{QUERY_STRING} ^page=([^&]*)&s=(.*)$ [NC]
RewriteRule ^search\.php/?$ /Search/%2/page%1.html? [R,L,NC]
R=301 will redirect with https status 301
L will make last rule
NC is for no case comparison
%1 and %2 are the query parameter values
I think you want make clean URI, so if user type url like this:
search/televisi/page4.html
it same as like search.php?s=dvd&page=1
Add this code:
RewriteEngine On
RewriteRule ^search/([a-z]+)/page([1-9]+).html$ search.php?s=$1&page=$2

Resources