Dont redirect if parameter is set - .htaccess

I want to redirect a url except when there is a parameter named login with the value false.
Tested it on several regexp testers and it works like I want.
But when it comes to the server it doesn't.
I always get redirected to ?login=true.
Why?
RewriteRule (.*)write((?!/?login=false).)*$ ?login=true [L]

RewriteRule only matches against everything after the hostname and before the query string. You'll need to use RewriteCond and the %{QUERY_STRING} variable.
RewriteCond %{QUERY_STRING} !login=false
RewriteRule (.*)write$ ?login=true [L]
See the documentation for more information.

RewriteRule (.*)write$ ?login=true [QSA]
RewriteCond %{QUERY_STRING} !name=
RewriteRule .? - [S=1]
RewriteRule (.*)write$ ?login=true [L]
made it like this now
works for me

Related

ReWrite rule not working with two parametrs

im quite new to rewrite rules.
I can manage with one variable and thats it.
I have webpage Where the rewriterule is:
RewriteCond %{HTTP_HOST} ^www\.someserver\.com$
RewriteRule ^/?$ "http\:\/\/someserver\.com\/" [R=301,L]
RewriteRule ^(\d+)*$ ./index.php?comp=$1
RewriteRule ^(\d+)*/$ ./index.php?comp=$1
And it all work fine as it should. But now as i want 1 more variable into URL
i cant get it to work.
Right now the $1 is only numbers.
someserver.com/1554886
But i want two variable.
someserver.com/1554886-SOMENAME-WHATEVER-WORD-AND-HOW-MANY
But it wont show.
i tried smth like this:
RewriteRule ^([^-]*)-([^-]*)$ ./index.php?comp=$1&string=$2 [L]
How do i get it to work?
Do i have to make some changes in the php side as well?
everything what comes after the number part of the URL is there only for
SEO, the number is Unique
You need one more rule to handle two parameters:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(someserver\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(\d+)/?$ index.php?comp=$1 [L,QSA]
RewriteRule ^(\d+)-(.+)/?$ index.php?comp=$1&string=$2 [L,QSA]

.htaccess remove GET completely

I have dozens of redirects from an old page e.g. index.php?mode=1,2,3,0 and I want to get rid of all GET Params because the new page is anyways just plain html.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} mode=17,0,0,0,0$
RewriteRule (.*) /big-mamas-house/ [R=301,L]
I thought removing (.*) would already do the trick but then the rule is not applied anymore according to:
http://htaccess.madewithlove.be/
Your rule can simplified to:
RewriteCond %{QUERY_STRING} ^mode=17,0,0,0,0$
RewriteRule ^index\.php$ /big-mamas-house/? [R=301,L,NC]
? in the end is needed to strip off any previous query string.

Cut off parameter from redirect in htaccess

This silent redirect in htaccess:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
redirect a url that looks like:
example.com/album_showpage.php?pic_id=1906
to:
example.com/gallery/image_page.php?image_id=1906
that works great. But when the URL has a parameter like:
album_showpage.php?pic_id=1906&mode=prev
or
album_showpage.php?pic_id=1906&mode=next
the redirect wont work.
Question: How to cut of any parameter after pic_id=1906
thank you
You need match against the rest of the query string.
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]

URL rewriting via my .htaccess file

How do I change example.com/1 to example.com/?id=1
I've tried googling but I can only find code for example.com/?id=1 to example.com/1
I used a generator and got this, but it didn't work
RewriteEngine On
RewriteRule ^\?get\=$ / [L]
Thanks,
Isaac
It kind of depends on which way you want to do the rewrite - ie, what does the user type see, and what does the server do.
If you want the user to see "http://example.com/1" and internally the server provides "http://example.com/?id=1", then the following should work:
RewriteRule ^([0-9]+)$ /?id=$1
However, if you want the user see "http://example.com/?id=1", and internally the server provides "http://example.com/1", then the following, as per Jon Lin's answer, should do it:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1?
You can't match against the query string in a RewriteRule statement, you need to use a RewriteCond and the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^$ /%1? [L]
The ? is needed in the rule's target to remove the query string.

PHP RewriteRule detect if url contain "string" and replace

This may be a basic question regarding RewriteRule but I just counldn't make it work.
What I want to do is detect urls like:
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
mydomain/myapp/id/other/randomabc/perrito.php?param=somethingThatIdoUse
...
The part that I need to discart is from the /id/[all this]?param=somethingIdoUse and use the param if is possible or send the complete url as param so I can regex to get the param.
And have a rule that detect that /id/ exist and redirect to something like:
mydomain/myapp/other/manageRequest.php?params=somethingThatIdoUse
(the params I could get the whole url and strip it as string is no problem)
As well the application have different modules like:
mydomain/myapp/moduleOne/index.php
mydomain/myapp/moduleTwo/index.php
This have to keep working the same.
As far I've tried some of them like:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET /.*;.* HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://localhostdev/app/index.php %{REQUEST_URI}? [R=301,L]
RewriteEngine On
RewriteBase /spt/
RewriteCond %{REQUEST_FILENAME} !^id$ [NC]
RewriteRule ^(.*)$ index.php [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_URI} !/campaings/response\.php$
RewriteRule ^/something/(.*) /other/manageRequest.php? [L]
But nothing seamed to do kind of what I needed.
Thanks in advice
mydomain/myapp/id/some/random/url.html?param=somethingThatIdoUse
Here is an example using the above URL:
RewriteRule ^id/some/random/url.html/? /myapp/other/manageRequest.php [L,NC]
The query will be passed through unchanged to the substitution URL
well actually end up being really basic it worked with:
RewriteRule ^.*id.*$ handleRequest.php [NC,L]
I do get the params as they are sent!

Resources