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

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

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.

how to Rewrite Single Page url in .htaccess

I need to rewrite only 1 specific URL
from
http://www.domainname.com/index.php?route=payment/axis/callback
to
http://www.domainname.com/payment/axis/callback
I tried these two from stack overflow, I don't know why its not working
1st one :
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?route=payment/axis/callback [NC,L]
2nd one :
RewriteRule ^index.php?route=payment/axis/callback payment/axis/callback [L]
Try this:
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
See the full page here.
Hope it helps!
I wouldn't use .htaccess with RewriteRule, since there are often problems with it. A simple workaround (with PHP redirect):
<?php
if($_GET['route'] == 'payment/axis/callback') {
header("Location: http://www.domainname.com/payment/axis/callback");
}
?>
You can either use h0ch5tr4355's workaround or you can try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=payment/axis/callback$
RewriteCond %{SCRIPT_FILENAME} index.php
RewriteRule (.*) /payment/axis/callback [NC,QSD]
If you instead of a rewrite would like it to redirect to the new url you can add R=301 to the flags of the RewriteRule.

htaccess redirect specific url to another domain

I want to redirect specific URL with params to another domain
http://domain.tld/buy/?w=X1234 to http://anotherdomain.tld/product.php?id=X1234
Here my htaccess
RewriteCond %{REQUEST_URI} !^/*buy/(.*)$ [NC]
RewriteRule ^/*buy/?w=([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
but not working. maybe the problem is param ?w=
Thank you
UPDATE: i solve the problem by using Query string, after reading How to REGEX and .htaccess rewrite url
RewriteCond %{QUERY_STRING} ^w=([a-zA-Z0-9]+)$
RewriteRule ^/*buy/$ http://anotherdomain.tld/product.php?id=%1 [NC,L,R=301]
Thank you stackoverflow and folks! i m start understanding regex from here ^_^
That's right, your params won't be picked up, but they can be passed on.
Something like;
RewriteRule ^buy/([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
Where your original url would be; /buy/X1234

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!

.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