how to generate this url from a sample with mod_rewrite? - .htaccess

i want to rewrite
example.com/login?id=login&says=invalid&usr=username
request to
example.com/login&says=invalid&usr=username
and i am using this rules .
RewriteCond %{THE_REQUEST} \login?id=([^\ ]*)
RewriteRule ^$ /%1? [R,L]
but it says internal server error (500)
what is wrong with it? even the
RewriteCond %{THE_REQUEST} login\?id=([^\ ]*)
RewriteRule ^$ /%1? [R,L]
do not work .
i tried many thing and no result.
note that login file is not a real file but it have its rule that
example.com/login&says=invalid&usr=username
works fine.
**#loop :**
the page
example.com/login&says=invalid&usr=username
works fine but if system add another thing to it it will show 404
for example if system want to do the same thing again ( adding ?id=login&says=invalid&usr=username to end of address ) it shows 404
example.com/login&says=invalid&usr=username?id=login&says=invalid&usr=username

You can replace your with this rule:
RewriteCond %{THE_REQUEST} /(login[^?]*)\?id=[^&\s]+(&\S*)?\s [NC]
RewriteRule ^ /%1%2? [NE,R,L]

Related

htaccess rewrite to wildcard subdomain explained

dear community.
Today i made this .htaccess rewrite rules ( found some part on the internet and added some by myself )
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php
When i request some.site.ru - site.ru/some.php content triggered. It's fine. And when i request some.site.ru/readme.txt i see site.ru/readme.txt content. And i dont quite understand why its working fine.
I thought RewriteRule ^ %1.php makes all requests to go through *.php files. Am i wrong?
Additionally i cant understand RewriteRule ^ this part. Is it the same as RewriteRule ^(.*)$ ?
This is your rule:
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php
Due to presence of faulty pattern in front of RewriteCond %{HTTP_HOST} it is a DO NOTHING rule, in other words this rule will never execute because of presence of a / in 2nd condition which can never be true because %{HTTP_HOST} matches only the host name part of a web request. So %{HTTP_HOST} can only match www.example.ru or example.ru.
So it is obvious that /readme.txt URL is working fine without any rewrite.

Remove parenthesis from htaccess (Helicon Ape)

I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

Why does the following htacces directive not work as expected?

I have the following redirects:
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=409$
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]
RewriteCond %{REQUEST_URI} foo_Bar\.pdf$ [NC]
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]
The first one works fine.
But the second one is not directing, when I open this url: domain.eu/fileadmin/images/foo_Bar.pdf (showing an 404 instead, thats why I want to redirect).
I've also tried to add a ^(.*) before the filename, but it doesnt work either. I've even tried to enter full url as the request uri, no luck. What am I missing?
Thanks
It will redirect www.yourdomain.com/index.php?id=409 to http://www.domain.eu/index.php?id=4
And the second will redirect if your site address ends with .foo_Bar.pdf redirects to http://www.domain.eu/index.php?id=4
You need to change the second one to:
RewriteCond %{REQUEST_URI} foo_Bar\.pdf$ [NC]
RewriteRule ^(.*)$ http://www.domain.eu/index.php\?id=4 [R=301,L]

Redirecting %{QUERY_STRING} to friendly url

I'm trying to add one last improvement (regarding htaccess), and that is that I would like it to redirect /?mod=inicio to /inicio. So I'm trying to do it with the following code, but It keeps building the url like this /inicio?mod=inicio
RewriteCond %{QUERY_STRING} ^mod=([a-z]+)$ [NC]
RewriteRule .* /%1 [L]
Same thing with extra parameters: from /?mod=x&type=y-z to /x/y-z
Try these rules instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)&type=([a-z\-]+)
RewriteRule ^ /%1/%2? [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)($|\ )
RewriteRule ^ /%1? [L]
The key is to match against the actual request instead of the URI because your other rules (from your previous question) will cause these rewrites to match, they'll conflict with each other. The other key point is to include a ? at the end of the targets (e.g. /%1/%2?) which makes it so the query string doesn't get appended.

HtAccess rewriting, removing a part of the text between the first two slashes and adding index.php after it

I got the following url:
127.0.0.1/abc_123456/default/index/index/
Which should be rewritten to:
127.0.0.1/123456/index.php/default/index/index/
So remove abc_ and add index.php after it. Problem is that the digits are variable, but the abc_ isn't.
I had the following rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_
RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2
But that resulted in the url being rewritten to:
127.0.0.1/123456/default/index/index.php/index/
Seems like I'm almost there, but I can't figure it out.
Thanks in advance
Use this simple rule:
RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
Try
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+) [NC]
RewriteRule ^ /%1/index.php/%2 [L]
EDIT
However, you are right about the other rule, that's the one giving the error; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 That one is used to rewrite if the page does not contain the /abc_123456/
Add an extra condition to that rule as below
#if not abc_
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ [NC]
#if not already index.php
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
for example, if you need similar solution, when visited this url:
http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
to redirected visitor to
http://yoursite.com/subpage1/subpage2/
then see link - http://stackoverflow.com/a/15680832/2215124

Resources