RewriteRule for the same file which is currently targeted - .htaccess

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

Related

add string at the end of URL using htaccess which has query string also

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.

How to redirect all the requests to check.php except of sample.com/index.html and sample.com/?i=1

I'm trying to redirect all the requests such as example.com/123 or example.com/any-url to check.php. But I need to not redirect these two requests: example.com/ and example.com/?i=1.
Here is my .htaccess:
RewriteEngine on
RewriteRule ^(|?i=1)($|/) - [L]
RewriteRule .* check.php
But this one still redirects everything to check.php What have I done wrong?
RewriteRule ^(|?i=1)($|/) - [L]
You can't match the query string with the RewriteRule pattern. Only the URL-path (which notably excludes the query string) can be matched here, you need to use a RewriteCond directive to match the query string. (However, that regex looks invalid and I would have expected it to have generated a 500 error? That is, if it's being executed at all??)
Try the following instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^i=1$
RewriteRule !^(index\.html)?$ check.php [L]
For all requests other than the document root (and /index.html) that do not contain the query string i=1 then rewrite to check.php.
Note that this is an internal rewrite, as opposed to an external redirect.

How to mod rewrite this URL correctly?

I am adding a REST API to my application. The REST API is located at /api of my app. I want to route all requests correctly including requests that have parameters. I have the following htaccess rules which are working for the most part:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>
Using these rules, I can now go to:
mysite.com/api/projects/get and it will correctly give me back JSON data as if I would have accessed it the regular way mysite.com?url=api/projects/get
My problem begins when I try to add parameters to the request.
My application will already handle parameters. So if I were to go to: mysite.com/api/projects&type=1, it will give me back the projects of type 1. However, I think it is expected that the URL would look more like: mysite.com/api/projects?type=1 (note the ? instead of the &)
How would I modify my htaccess rules to handle this?
I was trying the following but with no luck:
RewriteRule ^(.*)/?(.*)$ index.php?url=$1&$2 [L]
You'll need to use the Query String Append flag, as shown below:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Per the documentation:
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.

mod_rewrite and redirect causing loop

I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

How do I redirect a url with parameters

How do I redirect a url with parameters
I will like to write this url:
http://mydomain.com/dk/silkeborg?latitude=56.1631229&longitude=9.536976500000037&zoom=14
and redirect it to
http://mydomain.com/index.php?country=dk&city=silkeborg&latitude=56.1631229&longitude=9.536976500000037&zoom=14
To day I use the below rule, that don't take any parameters
RewriteRule (dk|de)/(.*) index.php?country=$1&city=$2 [NC]
Please help me
You do need to use [QSA] because you're rewriting the query (it's enabled by default unless you rewrite the query). Here we're capturing the country code and passing it as a var. Then QSA is catching the rest of the variables and adding them to the query string.
RewriteCond %{REQUEST_URI} ^/(dk|de)/ [NC]
RewriteRule .* index.php?country=%1 [QSA,L]
To use your existing rule, you would append [QSA]
RewriteRule (dk|de)/(.*) index.php?country=$1&city=$2 [QSA,NC,L]
When the page loads you can access the country via $_GET['country'] and city via $_GET['city'].

Resources