htaccess remove specific get parameter from url - apache

I'm using Google OAuth to connect to the dashboard, the problem is that when the GET &scope parameter of the callback contains a URL like "https://www.googleapis.com/auth/userinfo.profile", the host return an Error 403.
So after many researches the only solution I found is to remove scope from the url.
I want to change the callback url from this:
https://msite.com/?code=xxx&scope=email%20profile%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email
To this
https://msite.com/?code=xxx&scope=true
Here is my basic htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?a=$1 [QSA,L]

You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /\?code=([^&]+)&scope=email.+ [NC]
RewriteRule ^$ /?code=%1&scope=true [L,R=301]
This will redirect your URLs from
/?code=Sometext&scope=emailSometext
To
/?code=Sometext&scope=true

Related

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

URL Rewriting Using .htaccess for PHP

I want to convert this URL example.com/post.php?id=12345&title=xyz to example.com/12345/xyz.
I am able to do example.com/12345 but I can't get /xyz.
RewriteEngine On
RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?id=$1
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?id=$1
With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
This solution assumes that you are hitting example.com/post.php?id=12345&title=xyz sample url in browser and want to change it to example.com/12345/xyz
##Enabling engine here.
RewriteEngine ON
##Extrenal redirect 301 to mentioned url by OP in question.
RewriteCond %{THE_REQUEST} \s/post\.php\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite to post.php with query strings.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ post.php?id=$1&title=$2 [QSA,L]

.htaccess Rewrite Rule does not work correctly with index.php?path

Hi I have the following old url
www.domain.de/index.php?leistungen
and the new one is
www.domain.de/leistungen
I tried the following RewriteRuile like I did it millions of times.
RewriteRule ^index.php?leistungen /leistungen/ [L,R=301]
But in this case I got the following result:
www.domain.de/path/?leistungen=
And it it routes to the root url
What is the issue in this case?
Thanks in advance.
You may use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?$1 [L,QSA]

Apache, url rewrite

I have the following .htaccess file inside my root directory:
RewriteEngine On
<If "%{SERVER_NAME} = 'example.com'">
# The actual condition is really long one so I replaced it for illustration.
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC]
RewriteRule .* http://m.example.com%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/$ [NC]
RewriteRule .* %{REQUEST_SCHEME}://%{SERVER_NAME}/?title=%1 [L]
</If>
<Else>
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/$ [NC]
RewriteRule .* %{REQUEST_SCHEME}://%{SERVER_NAME}/?title=%1 [L]
DirectoryIndex /m/index.htm /m/index.php /m/index.html
</Else>
So as you can see its a pretty much simple way to redirect users of mobile devices to m.example.com and if that is the case I change the DirectoryIndex, few notes:
The reason I duplicate the same RewriteCond/RewriteRule is because I can't use them outside <If>/<Else> blocks.
The way I redirect(if/else) is not the question and I'm pretty sure not the best way, but if anyone have suggestions I'm all ears.
The problem:
If I access example.com/Article-Name on desktop browser there will be internal redirection to: example.com/?title=articleName but I won't see example.com/?title=articleName at my url bar as expected.
The exception:
If I access m.example.com/Article-Name my url bar is now m.example.com/?title=articleName.
I was expecting the same behavior as the desktop (internal redirection not redirection I can see on my url bar), I'm pretty sure its because the DirectoryIndex but I don't how to prove it or solve this issue, if anyone have a solution I will be very thankful.
The main issue with your code is that you're feeding it with an URL that is not the same as the current URL then, what happens is that it ignores the internal and make it an external redirect.
Another very important thing is that you forgot %{HTTP_USER_AGENT} to match against the mobile browsers.
Here is an idea to resolve your issue:
RewriteEngine On
# First we verify if its a mobile device and
# if its not already on the mobile subdomain
RewriteCond %{HTTP_HOST} !^m\.domain\.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC]
RewriteRule ^ http://m.example.com%{REQUEST_URI} [R,L]
# here we deal with all the internal redirects for
# the mobile subdomain
RewriteRule ^m/index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^m\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /m/index.php?title=$1 [L]
# here we deal with the normal website
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?title=$1 [L]
Your doing internal redirects, if you want to redirect to a new URL use external ones
Replace [L] with [L,R=302]
or 301 if you want the result to be cached

htaccess issue causing internal server error too many redirects

so I am playing with .htaccess to have clean URLs in my codeigniter app.
in short, i am trying to:
1) remove index.php in urls (redirect permanent)
http://localhost/directory/index.php*
to
http://localhost/directory/*
http://my.domain.com/index.php*
to
http://my.domain.com/*
2) rewrite requests for certain controllers to index.php/[controller_name]
http://localhost/directory/controller1*
to
http://localhost/directory/index.php/controller1*
http://my.domain.com/controller2*
to
http://my.domain.com/index.php/controller2*
my htaccess file currently goes like this:
Options +FollowSymlinks
RewriteEngine On
#RewriteBase /
# Redirect index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^(.*)index\.php((/)(.*))?$ /$1/$4 [R=301,L]
first issue:
this does not work for http://localhost/dir/index.php/controller1.
instead redirecting to http://localhost/dir/controller1, it redirects to http://localhost//controller1 ($1 return empty string?)
# Rewrite CI certain controllers
RewriteCond %{THE_REQUEST} directory/(home|other_controller) [NC]
RewriteRule ^(.*)(home|other_controller)(.*)$ /$1index.php/$2$3 [NC,L]
second issue:
this does not work for http://localhost/dir/home gives internal server error (too many redirects).
but if I test added R=301 code, it successfully redirect to http://localhost/dir/index.php/home. but this is not my intention to redirect, I only need to rewrite it.
please advise.. :)
Try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You can place this in the directory the bootstrap file (index.php) is in.
If you have FastCGI implementation, you need to add a question mark in the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]