I'm trying to rewrite all urls of the following pattern:
http://example.com/csfdg/anything
into http://example.com/csfdg/index.html
my .htaccess file at the root level contains this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule \/csfdg\/.* /csfdg/index.html [L]
The checker at http://htaccess.madewithlove.be/ tells me that it would rewrite my URLs the way I want, but when I go to http://example.com/csfdg/anything I just get a 404. It's very hard to tell what's going on, but I know that the RewriteEngine is working because if I mess with it enough I can get 500 errors to happen :)
Any thoughts? Thanks
You can use this :
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.html$
RewriteRule ^([^/]+)/.+$ /$1/index.html [L]
RewriteConditon is important here to avoid rewriting /foo/index.html to itself.
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.
I read some tutorials about getting clean urls. I tried some of the codes they gave in the tutorials but I can't get a simple example to run...
I would like to change: http://domain.com/brigandze/mannen/index.php?p=0
To: http://domain.com/brigandze/mannen/0/
I used this code and put it in the directory domain.com/brigandze/mannen/
I have this code in my htaccesfile:
RewriteEngine on
RewriteRule ^([0-9]+)/?$ index.php?p=$1 [NC,L]
You need to either set your RewriteBase correctly or use the full path in your RewriteRule (see here for RewriteBase documentation).
RewriteBase /brigandze/mannen/
RewriteEngine on
RewriteRule ^([0-9]+)/?$ index.php?p=$1 [NC,L]
or
RewriteEngine on
RewriteRule ^brigandze/mannen/([0-9]+)/?$ brigandze/mannen/index.php?p=$1 [NC,L]
The problem is that the web server looks at URLs starting from the DocumentRoot unless you tell it otherwise. In your case, that means that your rule is trying to rewrite
http://domain.com/0/
By adding the RewriteBase, it will look at the correct place.
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]
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]