.htaccess rewrite URL with a question mark "?" - .htaccess

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

Related

Redirect URL changing QUERY string parameter ? to & with .htaccess

I am trying to change this url:
http://blabla.nl/download/?url=https://www.bla.com/see?v=345345&type=download
to this:
http://blabla.nl/download/?url=https://www.bla.com/see&v=345345&type=download
Using .htaccess
so the ? needs to change to an &.
at the moment i am not very succesfull fixing this.
You have to do it this way:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(url=https://www\.youtube\.com/watch)\?(v=[A-Za-z0-9]+&type=downloa‌​d)
RewriteRule ^/?8/downloadff/?$ /8/downloadff/?%1&%2 [R=301,NC,L]
I assume that the number 345345 is always a number an will be different all the times but the rest keeps the same.
Put this as .htaccess file on http://example.nl/ or http://example.nl/download/
Try this:
RewriteEngine On
RewriteRule ^download/?url=https://www.bla.com/see&v=345345&type=download/?$ download/?url=https://www.bla.com/see?v=345345&type=download [NC,L]
I hope it works for you.

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

Question mark in 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`

mod_rewrite remove query string on top of a rewrite rule

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.
This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

Resources