Getting a rewriterule syntax to be correct - regex

I'm trying to write a rewriterule to put into my .htaccess. What i need accomplished is:
https://a.b.com/?bunch_of_stuff
to become
https://www.b.com/?bunch_of_stuff
i.e., in the URL, I want to lose the subdomain part (the 'a.') and replace it with 'www'.
I've tried a bunch of stuff but i keep getting the original URL back. I'm sure this is easy and i'm just not seeing it, so perhaps a fresh pair of eyes can straighten this out.
Thank you very much in advance.

Have you tried something like this?
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^a\.b\.com [NC]
RewriteRule ^(.*)$ https://www.b.com/$1 [L,R=301]

Related

Apache Rewrite to remove index.php?

I am trying to rewrite my URL's to remove index.php? but I'm struggling a little to get it to work. The closest I can get is the answer here: remove question mark from 301 redirect using htaccess when the user enters the old URL
I need to convert the URLs to pretty URLs on the way out, and rewrite them back to the proper URL on the way in. The structure of the URLs is as follows:
https://sub.domain.com/index.php?/folder1/folder2-etc
Using the code from the referenced answer results in a double forward slash:
https://sub.domain.com//folder1/folder2-etc
The rewrite rules I'm using from the referenced answer are:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} \s/+\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]
I suspect I know how to solve the first bit, but I'm struggling to understand the second rule for the internal forward.
Additionally, I'm wondering if this is the best way to do this. I'm currently running an Apache backend behind an Nginx reverse proxy. Would I be better doing the rewrite on the Nginx side and the internal forward on Apache?
EDIT:
Complication: I've noticed an additional structure to complicate things. Some URLs appear to have https://sub.domain.com/picture.php?/folder1/folder2-etc
For these, I'd be quite happy to keep 'picture' and just remove the .php? bit.
I'm guessing that for the first bit, Id need to do something like the following:
RewriteCond %{THE_REQUEST} \s/+index\.php\?/([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+picture\.php\?/([^\s&]+) [NC]
RewriteRule ^(.*)$ /picture/%1 [R=301,L]
But have no idea where to start with the opposite.... ie converting pretty urls back to standard. It would help if the following section could be explained to me?
^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]
RewriteRule ^/*picture/(.*)$ /picture.php?/$1 [L]
RewriteRule ^/*(?!/*index\.php$)(.*)$ /index.php?/$1 [L]
should do the trick. I wasn't able to test it yet though.
I only used the [L] last flag to stop applying rules on match. The QSA query string append flag doesn't seem to make sense as you don't seem to use ?key=value&... syntax anyway. Also dunno if you actually need the NC case-insensitive flag...
Side note:
I hope your php files don't serve paths with .. in them, as that would allow people to read arbitrary files from disk, e.g. /picture/../../../etc/passwd
Apologies, but as it turns out, the main reason I can't get anything to work is due to the use of relative URLs and dynamically generated links within the PHP. Not something I can change unfortunately. The not perfect URLs are something I'm going to have to live with. For reference, the app I'm using is Piwigo

RewriteRule - URL from Ugly to Pretty with variables not parsing correctly

I'd like some help rewriting an ugly url (http://example.com/image.php?id=1&name=Dog) to a pretty url (http://example.com/1/Dog) using Regex.
There are a lot of questions about the reverse version of this, and some other versions of the one I need. Nonetheless, they do not work.
RewriteRule ^image.php\?id=(.*)&name=(.*)$ http://example.com/$1/$2? [R=301,L]
This is the Regex I came up with. Testing it on Regex101 seems to be fine (?) https://regex101.com/r/uUlccx/2
It should be the reverse of this one:
RewriteRule ^([^/]*)/([^/]*)$ /image.php?id=$1&name=$2 [L]
But it seems like it isn't. Anyone know how the RewriteRule should be?
Edit: http://htaccess.mwl.be?share=47b41b23-1752-5a26-8432-196c95d1d669 htaccess tester
Edit 2: I got a little bit further, but it still doesn't seem to be working completely. The second variable is not taken in to the rewrite:
RewriteCond %{QUERY_STRING} ^(.*&)?id=(.*)&name=(.*)$
RewriteRule ^image\.php$ http://example.com/%1/%2/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^image\.php$ http://example.com/%1/%2/? [R=301,L]
This seems to be the solution. Query values cannot be found in the normal rewrite rule?

How to write all domain name redirects in one line

I think it should be possible to write most domain name redirects in one line,
can you tell me if you think this is correct?
RewriteCond %{HTTP_HOST} ^(www\.myexample\.fr|myexample\.fr|www\.myexample\.com|myexample\.com)$
RewriteRule ^(.*)$ http://www.my-example.com/$1 [L,R=301]
I can't test as I can't afford the website to crash, even for one minute, too many people are working on it.
Thank you,
i'm impatient to read your comments
You can simplify the conditions like this
RewriteCond %{HTTP_HOST} !=www.my-example.com
RewriteRule ^(.*)$ http://www.my-example.com/$1 [L,R=301]
i.e. is host is not www.my-example.com in request then redirect to www.my-example.com.

Redirecting to other domain with parameters

This is the initial url:
http://example.net/any/number/folders/param1/param2/value2/value1
and this is the url we need to be mapped to:
http://example.com/any/number/folders/param1.php?param1=value1&param2=value2
The script param1.php is under directory param1 and both exist.
I have these rules but don't work. Usually get a 404 error.
RewriteEngine On
RewriteBase /param1
RewriteCond %{REQUEST_URI} /param1
RewriteRule ^(.*)(param1)/(.*)/(.*)/(.*)$ http://example.com/$1/$2.php?$2=$5&$2=$3 [L,R=301,QSA]
I have been trying to make it work for hours and also searched for something similar. At this point I am not even sure if it is possible.
Any suggestion or solution will be really appreciated. Thank you.
I just made some minor adjustments, you may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /param1
RewriteRule ^(.*)(param1)/([^/]*)/([^/]*)/([^/]*)$ http://example.com/$1$2.php?$2=$5&$3=$4 [L,R=301]

Regex in .htaccess combining two negative conditions

could someone check this regex I wrote, it does what I wanted to achieve, but I'm not sure if it's the correct way to do it and if it's not slowing everything up
That's what it should do:
IF the URL Path is longer than the domain only
AND IF it doesn't contain the strings "/de" or "/en" at the beginning
THEN 301 it to the domain only
That's what I wrote:
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteCond %{HTTP_HOST} ^(?!.*(/de|/en))
RewriteRule .* http://example.com/ [L,R=301]
... is there a better way to achieve that?
Thanks!!
Urs
I am assuming that you did a "mindfart" and the second cond should read %{REQUEST_URI} otherwise it makes no sense as HTTP_HOST will never include a /
In runtime it makes very little difference, but it is easier to understand if written as
RewriteCond %{REQUEST_URI} !(/de|/en)