Rewrite/Forward URL in both directions ends in loop - regex

I want to rewrite or forward two URLs in both directions, because the software used does not allow this change in the called URL.
The URL that is called is "https://example.com/customerarea/cart.php?a=view" and should be forwarded to https://example.com/cart/ which is rewritten to the called URL.
Rewrite "cart/" to "customerarea/cart.php?a=view":
RewriteRule ^cart/$ customerarea/cart.php?a=view [QSA,L]
This rule works.
If I add the following condition and rule to forward from "customerarea/cart.php?a=view" to "cart/", it ends in a continuous loop.
RewriteCond %{REQUEST_URI} ^/customerarea/cart.php$
RewriteRule ^(.*)$ https://example.com/cart/ [R=301,L]

You need 2 rules like this:
RewriteCond %{THE_REQUEST} \s/+customerarea/cart\.php\?a=view [NC]
RewriteRule ^ /cart/? [R=301,L]
RewriteRule ^cart/$ /customerarea/cart.php?a=view [QSA,L]

Related

Handle one query parameter specifically

I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.

.htaccess rewrite w/ regular expression and ?lang=en parameter

I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.

How to exclude certain pages from the below rewrite condition

How to exclude /blog/wp-admin/ and /blog/wp-login.php from
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://examplesite.com/blog/$1 [R=301,L]
So that those will skip and stay on https and everything else will pass through.
In other words the below should not be affected by the rule:
_https://examplesite.com/blog/wp-admin/
_https://examplesite.com/blog/wp-login.php
How can this be achieved?
I have tried adding the below as a second condition but it doesn't do the trick:
RewriteCond %{REQUEST_URI} !^/?(wp-admin/|wp-login\.php)
This seems to work for me ok. However if the are browsing using the /blog in the URL then it needs to be in the condition also. Try this rule below how I have it.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(blog/)?(wp-admin/?$|wp-login\.php)
RewriteRule ^(.*)$ http://examplesite.com/blog/$1 [R=301,L]

How to properly write a RewriteCond for https and subdomains

Thus far I've gotten this far with my RewriteConditions but it's from hacking and slashing from internet examples. They seems to behave a little funny at times, like showing the directory in the url, so I assume I'm not doing it right.
The desired behaviour: I have multiple subdomains all pointing to the same root. Based on the subdomain value I want to redirect to a sub-folder in the root. I also want to force HTTPS. I have multiple subdomains, but I'll only show two for the sake of brevity. All the other conditions are virtually identical.
Here's my .htaccess code:`
RewriteEngine On
RewriteCond %{HTTP_HOST} purchase.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !purchase/
RewriteRule (.*) https://purchase.mydomain.com/purchase/$1 [L]
RewriteCond %{HTTP_HOST} booking.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !booking/
RewriteRule (.*) https://booking.mydomain.com/booking/$1 [L]
`
Thank you,
Mike
Change your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(purchase|booking)\.mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} =purchase.mydomain.com
RewriteRule ^((?!purchase/).*)$ /purchase/$1 [L]
RewriteCond %{HTTP_HOST} =booking.mydomain.com
RewriteRule ^((?!booking/).*)$ /booking/$1 [L]
http:// or https:// in target will redirect instead of rewriting.

New .htaccess puzzle

I have this link:
index.php/forums/viewforum/5/
Now, I want that "forums" word in URL to become dynamic such that which ever word I replace it with, it still redirects to the same URL.
For example, if I have:
ProductA/viewforum/5/
it redirects to:
forums/viewforum/5/
For example, if I have:
ProductB/viewforum/13/
it redirects to:
forums/viewforum/13/
In other words, if there's a "view forum" word in the URL, it should trigger this rewrite.
I already have a .htaccess that removes the index.php from the URL so the rewrite rule should consider that too.
Is it possible?
HTACCESS:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteEngine on
RewriteCond %{REQUEST_URI} (member)
RewriteRule ^(.*)$ http://%{SERVER_NAME}/404 [R=301,L]
RewriteRule ^[.*]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*)$ /home/ddco/public_html/index.php/$1? [L]
</IfModule>
You can use this rewrite rule:
RewriteCond %{THE_REQUEST} /forums/ [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
Also suggesting you to post your current .htaccess rules as well for further investigation.