.htaccess redirect with 2 parameters - apache

I am trying to get the following redirect done.
website.abc/index.php?page=foo // With one parameter
website.abc/foo
website.abc/index.php?page=foo&article=bar / With a second parameter
website.abc/foo/bar
And it has to be redirected backwards as well.
If i type website.abc/foo/bar it should internally being redirected to index.php?page=foo&article=bar
I can get it to work with one parameter, but as soon as i'm using the second, the URL looks like this and gives me the 404:
website.abc/foo&article=bar.
I have tried many different ways of making it possible, but nothing worked.
Does anyone know how to do this?

Go like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php [NC]
RewriteCond %{QUERY_STRING} \bpage=([^&]+)\b [NC]
RewriteCond %{QUERY_STRING} \barticle=([^&]+)\b [NC]
RewriteRule .* /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_URI} ^/index\.php [NC]
RewriteCond %{QUERY_STRING} \bpage=([^&]+)\b [NC]
RewriteRule .* /%1/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(?:/([^/]+)/?)?$ index.php?page=$1&article=$2 [L,NC]

Related

.htaccess redirect path & subdomain as query parameter

I want to redirect a path to a file as query parameter.
For ex:
http://subdomain.example.com/helloworld/
Should redirected to
/index.php?domainParam=subdomain&param1=helloworld
I have tried multiple ways but they either helps with subdomain or with the path. But I want both of them to be redirected as a parameter in a query string.
Like:
RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^$ index.php?domainParam=%1 [L]
RewriteCond %{HTTP_HOST} !^app\.example\.com$ [NC]
RewriteRule ^([^/]+)/? index.php?source=$1 [L]
But it is not helping when redirecting both of them as parameters.
Need your assistance to merge them both.
You may use this rule for this task:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(?!app\.)([^.]+)\.example\.com$ [NC]
RewriteRule .* index.php?domainParam=%1&param1=$0 [L,QSA]

.htaccess external & internal rewrite conflict

I'm trying to rewrite a query string to a path, like so:
http://example.com/?p=page1
to
http://example.com/page/page1
The internal redirect works and I can view the page at the second URL but as soon as I try to redirect the first URL to the second, I get a 'Too many redirects' error.
.htaccess:
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
The first two lines are working by themeselves. The addition of the last two lines causes the error.
Your rule is redirecting the uri back it itself that is why you got the redirect error.
You can use %{THE_REQUEST} or %{ENV_REDIRECT_STATUS} variables to avoid Too many redirect error .
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
or
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /\?p=.+ [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE]

RewriteRule changing the URL instead of mapping to a file

I've encounter a weird behavior that I cannot explain nor correct. I need to redirect every HTTP request to HTTPS. I've use the following code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# The query string in the rewrite is for testing purposes
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&http=%{HTTPS} [L]
So far, it works. Then, I need a single page to be HTTP, so I added some rewrite conditions :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/not-https
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/not-https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&https=%{HTTPS} [L]
Now, here what's happening. For some reasons, when accessing the /not-https page, it redirects to /index.php?url=not-https&/not-https&https=off
Here is a map of GET requests followed by the redirects / displayed URL.
GET: http://example.com/test
-> https://example.com/test with proper $_GET
GET: http://example.com/test.jpg
-> https://example.com/test.jpg with no $_GET (file exists)
GET: https://example.com/not-https
-> http://example.com/not-https
-> http://example.com/index.php?url=not-https&/not-https&https=off
My question is why does the not-https change the displayed URL (and therefor, mess up my application)?
It is happening because value of REQUEST_URI variable is changing to /index.php?... in last rule that makes condition !^/non-https succeed in 2nd rule and makes it execute that rule.
Change your 1st 2 rules to this:
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+not-https [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+not-https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Unlike REQUEST_URI variable THE_REQUEST doesn't change it's value after execution of other internal rewrites.

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

Mod_rewrite not redirecting all cases correctly

I always seem to have trouble with this and no matter what I try I can't get all cases to redirect correctly.
I currently have
example.com/anything
correctly redirects to
example.com/track.php?memb=anything
and basically any url with a filetype at the end should ignore all redirects. I also have a subdomain rule at the top which I think is implemented correctly to not interfere with what I am trying to add. All of this is currently working correctly with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)? [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* track.php?memb=%1 [L,E=END]
I need to add the following rule:
example.com/c/whatever
needs to redirect to
example.com/page2.php?c=whatever
After many attempts and modifications of some of the current rules, I can't seem to get it working but I think I'm close with
RewriteRule ^c/(.*)$ page2.php?c=$1 [L,E=END]
RewriteCond is only valid for the next RewriteRule. Also,
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)? [NC]
will match anything with a . in the name. Since you have a ? at the end, it won't matter if the extension has a valid name or not.
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$ [NC]
RewriteRule ^c/([^/]+)/? page2.php?c=$1 [L,E=END]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$ [NC]
RewriteRule ^([^/]+)/? track.php?memb=$1 [L,E=END]