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)
Related
i want rewrite my url with htaccess. So, i want for example that
https://sample.com/~X6y2
has a match and it passes all behind the '~' as parameter to https://sample.com/req.php?id=[here]
is it possible with the '~'-character? so, if not, i need an alternative. Maybe '#'-Charakter, like tiktok.
So, it should match e.g.
https://sample.com/~AZaz09,
https://sample.com/~0123
but not
https://sample.com/0123,
https://sample.com/AZaz09,
https://sample.com/XY/~Z1234,...
I've tried several regex but nothing has provided the desired function :(
Best of these is (but not working, idk why):
RewriteEngine on
RewriteRule ^.*\/[\~]([a-z][^.]*)$ req.php?id=$1 [NC,L]
I'm new to regex and htaccess, so it's a bit difficult for me. I would be grateful if someone would provide a working htaccess including regex. Thx ✌🏼️
You want this solution
Check this solution
Demo: https://regex101.com/r/GJPZym/1
After a lot of trying, I've found a solution that is workin just fine.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*[\~]([A-Za-z0-9_-]*)$ req.php?id=$1 [L,NC]
where ~ can be replaced with anything u want :)
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
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]
What is the difference in using ^ vs ^(.*)$ vs ^.*$ as wildcards in a RewriteRule?
My goal is to redirect http://carnarianism.com/ (anything) to the landing (default) page of http://carnarian.com/. I have found the following solutions, which all seem to work, so I wonder which is better for performance?
RewriteRule ^ http://carnarian.com/ [R=301,L]
RewriteRule ^.*$ http://carnarian.com/ [R=301,L]
RewriteRule ^(.*)$ http://carnarian.com/ [R=301,L]
All of these seem to work okay. This is my very first post on StackOverflow, most of the time I can find an answer just searching for it.
To be clear: ABOVE the questioned RewriteRule in my .htaccess is a RewriteCond and WWW Handler as follows:
RewriteEngine On
RewriteBase /
# FROM www. --TO-- NO www. See no-www.org
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} carnarianism\.com$ [NC]
########## The Above Questioned RewriteRule ??? ##########
RewriteRule ^ http://carnarian.com/ [R=301,L]
Note: I started this search with the following, but I did not want the following because the path was also passed, and I want it to go to the landing page only. Therefore, I know you need the parentheses to be able to use the $1 variable. I do not want the $1 variable.
RewriteRule ^(.*)$ http://carnarian.com/$1 [R=301,L]
^ makes none of the original URL accessible as backreferences. $0 is an empty string.
^.*$ makes the entire original URL accessible as the $0 backreference (so you can do e.g. http://example.com/oldurl.php?url=$0)
^(.*) makes the entire original URL accessible as both the $0 and $1 backreferences; it's usually used when you want to actually use the old URL in the replacement since it's more explicit about the use.
All of them match the same thing, but produce different backreference groups.
The one that is better performance wise is the one you have benchmarked yourself.
But since you are using a .htaccess file rather than having this configuration in the server directly (maybe via a VirtualHost?) which is parsed only once, it really doesn't matter. Parsing .htaccess files at every single request is much more time consuming than performing the regular expression by a factor of thousands.
If you care about performance you should never ever use .htaccess files and even disable their parsing with: AllowOverride None. Not disabling them, and having a request like: http://example.com/sites/css/theme/main.css Apache will still try to load all the following files:
.htaccess
sites/.htaccess
sites/css/.htaccess
sites/css/theme/.htaccess
It will generate system calls even if those file does not exist.
Trying therefore to improve your RewriteRule in an .htaccess file is like sneezing in the ocean in the hope of making it less salty. :)
Now, if you improved your setup to use server configuration and to answer your original question: ^.*$ might be more efficient than ^(.*)$ as less references needs to be created. Chance is high, however, that you can't measure it.
I'm trying to prevent, in this case WordPress, from rewriting certain URLs. In this case I'm trying to prevent it from ever handling a request in the uploads directory, and instead leave those to the server's 404 page. So I'm assuming it's as simple as adding the rule:
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
This rule should evaluate to false and make the chain of rules fail for those requests, thus stopping the rewrite. But no... Perhaps I need to match the cover the full string in my expression?
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/.*$
Nope, that's not it either. So after scratching my head I do a check of sanity. Perhaps something is wrong with the actual pattern. So I make a simple test case.
RewriteCond %{REQUEST_URI} ^/xyz/$
In this case, the rewrite happens if and only if the requested URL is /xyz/ and shows the server's 404 page for any other page. This is exactly what I expected. So I'll just stick in a ! to negate that pattern.
RewriteCond %{REQUEST_URI} !^/xyz/$
Now I'm expecting to see the exact opposite of the above condition. The rewrite should not happen for /xyz/ but for every other possible URL. Instead, the rewrite happens for every URL, both /xyz/ and others.
So, either the use of negated regexes in RewriteConds is broken in Apache, or there's something fundamental I don't understand about it. Which one is it?
The server is Apache2.
The file in its entirety:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
RewriteRule . /index.php [L]
</IfModule>
WordPress's default file plus my rule.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So, after a lot of irritation, I figured out the problem, sort of. As it turned out, the rule in my original question actually did exactly what it was supposed to. So did a number of other ways of doing the same thing, such as
RewriteRule ^wp-content/uploads/.*$ - [L]
(Mark rule as last if pattern matches) or
RewriteRule ^wp-content/uploads/.*$ - [S=1]
(Skip the next rule if pattern matches) as well as the negated rule in the question, as mentioned. All of those rules worked just fine, and returned control to Apache without rewriting.
The problem happened after those rules were processed. Instead, the problem was that I deleted a the default 404.shtml, 403.shtml etc templates that my host provided. If you don't have any .htaccess rewrites, that works just fine; the server will dish up its own default 404 page and everything works. (At least that's what I thought, but in actual fact it was the double error "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.")
When you do have a .htaccess, on the other hand, it is executed a second time for the 404 page. If the page is there, it will be used, but now, instead the request for 404.shtml was caught by the catch-all rule and rewritten to index.php. For this reason, all other suggestions I've gotten here, or elsewhere, have all failed because in the end the 404 page has been rewritten to index.php.
So, the solution was simply to restore the error templates. In retrospect it was pretty stupid to delete them, but I have this "start from scratch" mentality. Don't want anything seemingly unnecessary lying around. At least now I understand what was going on, which is what I wanted.
Finally a comment to Cecil: I never wanted to forbid access to anything, just stop the rewrite from taking place. Not that it matters much now, but I just wanted to clarify this.
If /wp-content/uploads/ is really the prefix of the requested URI path, your rule was supposed to work as expected.
But as it obviously doesn’t work, try not to match the path prefix of the full URI path but only the remaining path without the contextual per-directory path prefix, in case of the .htaccess file in the document root directory the URI path without the leading /:
RewriteCond $0 !^wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ /index.php [L]
If that doesn’t work neither, it would certainly help to get some insight into mod_rewrite’s rewriting process by using its logging feature. So set RewriteLogLevel to a level of at least 4, make your request and take a look at the entries in the log file specified with RewriteLog. There you can see how mod_rewrite handles your request and with RewriteLogLevel greater or equal to 4 you will also see the values of variables like %{REQUEST_URI}.
I have found many examples like this when taking a "WordPress First" approach. For example, adding:
ErrorDocument 404 /error-docs/404.html
to the .htaccess file takes care of the message ("Additionally, a 404 Not Found error...").
Came across this trying to do the same thing in a Drupal site, but might be the same for WP since it all goes through index.php. Negating index.php was the key. This sends everything to the new domain except old-domain.org/my_path_to_ignore:
RewriteCond %{REQUEST_URI} !^/my_path_to_ignore$
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{HTTP_HOST} ^old-domain\.org$ [NC]
RewriteRule ^(.*)$ http%{ENV:protossl}://new-domain.org/$1 [L,R=301]