For example:
ex.com/read?id=1
should open
ex.com/route.php?action=read&id=1
but url won`t changing.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)\?id=(.+)\sHTTP.*$
RewriteRule ^(.*)$ /route.php?action=$1&id=%2 [QSD,R=301,L,NE]
RewriteCond %{QUERY_STRING} ^action=(.*)&id=(.*)$
RewriteRule ^ /%1?id=%2 [QSD,L,NE]
First I match request that contains query string id=whatever then redirect it with new query string.
Then I match new URI which contains new query string action=whatever&id=whatever and redirect it internally to same original path.
QSD flag is is available in Apache version 2.4.0 and later https://httpd.apache.org/docs/trunk/rewrite/flags.html , I added to discard previous query string and prevent it to be appended.
Clear browser cache then test these rules .
UPDATE:
As per your comment , you want to open /route.php?action=read&id=1 internally while /read?id=1 in browser so , you could do what starkeen answered , but with specific query string and general URI it should look like this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^(.*)$ /route.php?action=$1 [QSA,L]
So if query string is existing and strat with id=whatever , /abc?id=123 will get internally from /route.php?action=abc&id=123 and the previous query string will be appending with new one
You can use the following RewriteRule in your /.htaccess :
RewriteEngine on
RewriteRule ^read/?$ /route.php?action=root [QSA,L]
This will rewrite /read?id=1 to /route.php?action=read&id=1 . You do not read to write &id=1 in the Rule's destination as QSA automatically adds it to the Rewrite destination.
Related
I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.
I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :
http://domain/index.php?folder=AB_CD
to an URL like this
http://domain/AB/CD/
How can I write the rule please ?
Try the following code in root/.htaccess :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
Explaination :
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
index.php?query_strings gets redirected to /query/strings, if the condition is met.
Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.
(Hope, this helps!)
I changed my website url structure
FROM :
http://mywebsite.com/?page=example.php
TO :
http://mywebsite.com/example.php
AND FROM :
http://mywebsite.com/?page=profile.php&u_i = 10
TO :
http://mywebsite.com/profile.php?u_i=10
So I add to the htaccess file this codes to convert from the old to the new structure to know lose the power of SEO of my old links:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=([^&]+)&u_i=([^&]+) [NC]
RewriteRule ^ /%1?u_i=%2 [NC,R,L]
My problem is that this code just solve the second case when there is a parameter in the URL , but the first case still not solved , I want to know if I miss something in that code , should I add or edit something to include the first case also ?
Simple, just add a "?" to the end of your second regular expression, to make it optional eg.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=([^&]+)(&u_i=[^&]+)? [NC]
RewriteRule ^ /%1?%2 [R,L]
first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.
I have a file like
www.domain.com/test.php
I want to write a rule that when ever this is called i am sending an argument to this file but I don't want them to show in the URL I.E. when the above path is in the browser the actual rule should be:
www.domain.com/test.php?state=yes
What should be the .htaccess rule for this?
Currently i have in my .htaccess
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^test\.php$ /test.php?view=index [NC,L]
But this is throwing a 500 server error???
I don't get a 500 error, what else do you have in your .htaccess?
Here's what you can use:
RewriteEngine on
RewriteBase /
RewriteRule ^test\.php test.php?view=index [NC,L,QSA]
The QSA flag was missing:
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
Consider the following rule:
RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.
source: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa