So I have this rule in my .htaccess file. It correctly stops the rewrite for the "files" directory but none of the other directories. I expected this to not redirect the files, pdf, img, and account directories.
RewriteCond %{REQUEST_URI} !^/(files|pdf|img|account).*$ [NC]
I also tried it with
RewriteCond %{REQUEST_URI} ^/[^(files|pdf|img|account)].*$ [NC]
This stops the redirecting for the all the requested directories but also for the mobile site so I think this just breaks the whole rule set I have.
My rule set is kind of long it so for redirecting from our main site to m.mainsite for mobile devices. If it'd be useful to see the whole set please let me know.
Thanks.
Instead of REQUEST_URI try THE_REQUEST variable as THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
RewriteCond %{THE_REQUEST} !/(files|pdf|img|account) [NC]
You can use these two condition statements instead of just the one. One for the directory and then one for anything inside the directory such as a file or another directory.
RewriteCond %{REQUEST_URI} !^/(files|pdf|img|account)/?$ [NC]
RewriteCond %{REQUEST_URI} !^/(files|pdf|img|account)/(.*)$ [NC]
I currently have this line in my .htaccess file making URLs "pretty" for pages.
RewriteRule ^([A-Za-z0-9-]+)/$ /directory/?type=page&title=$1 [L]
However, I sometimes have sub-pages (/page/sub-page/sub-page/), yet this line only supports one level (/page/).
How can I alter this code to support sub-pages (any amount) and make the title variable something like:
&title=page/sub-page/sub-page
/ being the seperator.
I know that this will definitely be a huge help:
RewriteRule loop in .htaccess not working
However, due to my lack of knowledge with regex and mod-rewrite, I don't know how to make it properly work with what I already have.
Try this:
AllowEncodedSlashes On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([/a-z0-9-]+)/?$ /directory/?type=page&title=$1 [NC,B,L]
Don't worry about A-Z because of the NC flag.
After doing some regex research, and doing some testing, it seems like this is the answer to my problem:
RewriteRule ^(([a-zA-Z0-9-]+)/(([a-zA-Z0-9-]+)\/?)+)$ /directory/?type=page&title=$1 [L]
I altered my previous code and allowed support for multiple sub-directories. Then I placed it into a capture group and placed that data into the title variable.
I thought I had this figured out but no dice... I'm attempting to rewrite a domain but preserve the subdirectory paths:
www.olddomain.com/sub/directory/page.php
TO
www.newdomain.com/sub/directory/page.php
The following two directives are both needed. The first is the attempt to accomplish the above. The second is inserting a hidden "index.php" before every URL.
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Curently, no matter what URL I visit on olddomain.com, it takes me to newdomain.com/index.php. Maybe it is inserting the "index.php" from the previous site... I inserted [L] to attempt it to stop parsing the .htaccess file but it doesn't like that either. Any direction would be much appreciated.
You need to make sure to remove any rules you have on your old site (if the site resides in a different place than your new domain), otherwise, you need to make sure that the redirect is the very first rule, right under the RewriteEngine On line.
Your rule:
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
is correct. And as long as there's nothing before it, it should work.
Make sure you've cleared your browser's cache.
I need some help on the htaccess subdomain to be pointed to specific file in a folder.
Inside the parent folder should contain individual php files to correctly path the right applications. Sounds simple here.
The url that the user has type should not change / redirect, instead it should only
"link" to the correct file at the background
Example when user type userbase.company.com it should point to
http://company.com/parent/userbase.php
Similarly, when user type userbase2.company.com it should point to
http://company.com/parent/userbase2.php
but no redirection should happen. url links should remain at
http://userbase2.company.com
folder would be the same, but not the file. Is it possible?
Currently I have this htaccess code:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^userbase.company.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.userbase.company.com$
RewriteRule ^(.*)$ \./parent/userbase.php/$1 [L]
I'm going have to put this at /public_folder/ right? Not at the /public_folder/userbase ?
This rule should do the job:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/parent/
RewriteCond %{HTTP_HOST} !=domain.com
RewriteCond %{HTTP_HOST} !=www.domain.com
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteRule (.*) /parent/%1.php%{REQUEST_URI} [L]
URL will remain unchanged in browser (means rewrite -- internal redirect).
It will only work for something.domain.com (not domain.com or www.domain.com).
It will rewrite this request http://user1.company.com/hello/pink/kitten.php into /parent/user1.php/hello/pink/kitten.php
If subdomain is complex (e.g. www.something.domain.com) then it will be reflected in rewrite as well: e.g. http://www.something.company.com/hello/pink/kitten.php will be rewritten to /parent/www.something.php/hello/pink/kitten.php. If such behaviour is undesired (I do not know your requirements for 100%) then you will need add 1 more condition to the rule.
Request for a home page (e.g. http://user1.company.com/) will be rewritten in this way: /parent/user1.php/
P.S.
Rule was tested before posting -- works fine, but you have to check and maybe tweak it if it needs to be working with your CodeIgniter app (sorry, I'm not familiar with that framework).
In any case this rule should be above your CodeIgniter rewrite rules (if there are such).
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]