How do I get apache RewriteRule working correctly for a subdomain? - apache

I just setup a subdomain with the following RewriteCond:
RewriteCond $1 !^search.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)$ search.php?q=$1 [L,NS]
I'm using the same rewrite condition on my main domain and it works perfectly. However, when I set it up on the subdomain, it simply outputs "index.php" when going to http://sub.domain.com
Every page on the subdomain outputs the page name in the body instead of processing the code, except for the search page, which appears to be working correctly.
What can I do to correct this issue?

I haven't played with your exact regex with mod_rewrite, but if I was looking at writing that regex in another engine, I would have to escape the slash. Also, given that $ is used to indicate a back reference, would that need escaping too (would your $ symbols in the regex be necessary as there is likely to be more text in the URI and it is not matched at the end of a string)?
I would try
RewriteCond $1 !^search.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^\/]+)$ search.php?q=$1 [L,NS]
One other thing. Normally $ at the end of a regex means "only match if this is the end of the string". So from that, if RewriteCond is matching on ^search.php$ but the URL is search.php?q=... then I would think that this wouldn't match because search.php is not the end of the string. So that would look like the following (assuming you don't need to change anything else from your original).
RewriteCond $1 !^search.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([^/]+)$ search.php?q=$1 [L,NS]

In the main config the path always begins with / and you need an absolute path:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^search.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]+)$ %{DOCUMENT_ROOT}/search.php?q=$1 [L]
In an .htaccess you need a RewriteBase which is stripped from the url (no / in the Rule now) and the path is relative.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^search.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ search.php?q=$1 [L]

Several things come to mind here:
I have a few suggestions/comments/gotchas. Hopefully one of them is useful to you:
Make sure search.php isn't just echoing out its $_GET parameters. While this sounds obvious in retrospect, it's one of the more overlooked solutions.
RewriteRule works slightly differently when you specify it in a server configuration file than if you specify it in an .htaccess. Specifically, ^/ is wrong in a server config version as the entire URL is used (http://sub.domain.com/blah).
Make sure no other rewrite rules are being processed for this subdomain first, either in the main httpd.conf / apache2.conf or .htaccess.
Make sure RewriteEngine On appears in your configuration, as it is activated per-VirtualHost.
The NS flag will ignore redirects done using a relative Redirect or relative RewriteRule.

It sounds like the pattern '^/?([^/]+)$' may not be matching at all.
I'd activate RewriteLog, crank RewriteLogLevel to level 3 or above, and see if your pattern is matching at all. If not, start with a simpler pattern, and then work your way to a more complex pattern.
Or, something else is matching the pattern, so the request never gets to 'RewriteRule ^/?([^/]+)$' at all. You will see this in the RewriteLog.
I believe I recently had a problem where '^/' didn't match in certain cases on a Virtual Host. But '/' worked. The folks in the #httpd on Freenode.org helped me. If I can find this in my notes, I'll post it here.

Related

Editing .htaccess file to modify URL

I'm trying to modify my .htaccess file to modify my URL and have tried many methods but cannot achieve exactly what I want. For example I have this URL:
http://mywebsite.com/FOLDER/index.php?id=5
Now I want it to look like:
http://mywebsite.com/FOLDER/5
or
http://mywebsite.com/FOLDER/ID/5
My .htaccess contains the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^index/([0-9]+)/([0-9a-zA-Z_-]+) index.php?id=$1 [NC]
I cannot figure out what's wrong. Thanks.
You can use:
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+FOLDER/index\.php\?id=(\d+) [NC]
RewriteRule ^ /FOLDER/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^FOLDER/(\d+)/?$ FOLDER/index.php?id=$1 [L,QSA,NC]
The first argument of RewriteRule is what the incoming url without domain and without preceding paths (more on that later) is going to be matched against. This url is, in your case, http://mywebsite.com/FOLDER/5. Assuming that your .htaccess file is in your DocumentRoot, the regex will match against FOLDER/5.
You are currently trying to match FOLDER/5 with ^index/([0-9]+)/([0-9a-zA-Z_-]+), which is not going to work. A better regex would be ^(.*)/([0-9]+)$ or ^(.*)/ID/([0-9]+)$. You can then rewrite to $1/index.php?id=$2. I would recommend using the [L] flag to stop rewriting for this round to avoid common problems with multiple rules matching while you do not expect them to.
Besides this, make sure that your .htaccess files are being read (e.g. by checking that if you enter garbage, you get a 500 internal server error), that mod_rewrite is enabled, that you are allowed to override FileInfo. You also may need to turn AcceptPathInfo off.

How to avoid loops in the exceptions of generic regex rules?

Need to use flags? RewriteCond? The mod_rewrite is enabled and wroking, my problem is how to express my needs: there are a generic rule and some exceptions to the rule, the exceptions not working, and solution must avoid loops.
My /var/www before to use mod_rewrite, was working with usual folders like /var/www/wiki, /var/www/foo, /var/www/bar (each with its index). I need to preserve this behaviour.
Now, with mod_rewrite, I am using a /var/www/index.php to redirect some special strings, that match ^([a-z0-9]+), such as http://myDomain/foo123 or http://myDomain/wiikii. But it matches also "wiki" and "foo", that need exception handling.
My /var/www/.htaccess is
RewriteEngine on
RewriteRule ^/?(foo|bar)([0-9]+) index.php?$1=$2
RewriteRule ^/?([a-z0-9]+) index.php?foobar=$1
so, something like http://myDomain/wiki is redirect to index.php?foobar=wiki, but I need it with no redirection, need Apache going to /wiki.
PS: I try some variations, and some changes into index.php (working with redirections internally), but need elegant and secure Apache Rewrite solution, avoiding loops.
Usually conditions are the way to go, checking against REQUEST_URI.
RewriteCond %{REQUEST_URI} !^/(wiki|foo)$ <-- added
RewriteRule ^([a-z0-9]+)$ index.php?foobar=$1 [L]
An alternative is using e.g. negative lookahead in your regex. For example:
RewriteRule ^(?!(?:foo|wiki)$)([a-z0-9]+) ...
But I find any of the zero width assertions are quite complicated/fragile/unintuitive even if normal regexes come quite easy to you.
?! is negative lookahead
?: is just non-capturing parens for the "or"
The most confusing part about these is that pcre tries hard to find a way to NOT satisfy the negative lookahead, in other words it tries hard to have a succesful overall match.
If you don't want any redirection for wiki folder (and others), you can add a condition
RewriteEngine on
# If requested url is an existing file or folder, don't touch it
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]
# If we reach here, this means it's not a file or folder, we can rewrite...
RewriteRule ^(foo|bar)([0-9]+)$ index.php?$1=$2 [L]
RewriteRule ^([a-z0-9]+)$ index.php?foobar=$1 [L]
Also, you can use sort of exception instead, if you don't want it to be apply on all folders/files
RewriteEngine on
# If requested url is a folder/file in exception list, don't touch it
RewriteCond %{REQUEST_URI} ^/(foo|bar|example_file\.html|wiki)$
RewriteRule . - [L]
# If we reach here, this means it's not a file or folder in exception list, we can rewrite...
RewriteRule ^(foo|bar)([0-9]+)$ index.php?$1=$2 [L]
RewriteRule ^([a-z0-9]+)$ index.php?foobar=$1 [L]

How to use mod_rewrite to direct anything but PHP pages to index.php?

I have a question that is a bit confusing. I'm trying to understand mod_rewrite but I am pretty confused by it.
What I'm trying to do is redirect all URLs such as /settings/account or /user/user123 to index.php. A PHP script (which I already created) uses $_SERVER['REQUEST_URI'] to break the URL down into pieces then uses include. For example it would include settings.php?page=account or user.php?uid=user123.
But if the URL is /settings.php?page=account or /user.php?uid=user123 I don't want it to be redirected through index. In other words, if the URl has a file extension, just go to that file, but if it doesn't (like /settings) go to index.php to process where it should go.
How can this be done? Thanks in advance for any suggestions!
If you don't want to use the commonplace redirect-every-url-which-has-no-corresponding-file approach, you can also try your approach. To redirect anything without dot to a common script, use:
RewriteRule ^([^.]*)$ index.php?path=$1
You might want to exclude some URLs still, like images/ or css/ etc. Use a simple RewriteCond before the rule then:
RewriteCond %{REQUEST_URI} !images/
RewriteCond %{REQUEST_URI} !css/
What you need is a RewriteCond directive with !-f and !-d patterns.
RewriteCond checks whether a user's HTTP request matches provided condition. In your case, you would want to check if a requested file (which is provided in a variable %{REQUEST_FILENAME}) already exists on your document root using the !-f postfix. Similarly, if you want indexing in directories, you can add the same pattern with !-d, which will check if requested directory is on your document root.
So these two lines before the actual RewriteRule would do the trick:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

How do I redirect a specific URL pattern when Drupal Clean URLs are on?

I have a Drupal 5.23 installation using clean URLs with Apache and the mod_rewrite module. I am using an .htaccess file for the clean URLs functionality with the following configuration:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
I am going to be disabling the Localization/Internationalization plugins on the website, which is going to change every single page's URL on the website from http://www.example.com/en/url-to-a-page to http://www.example.com/url-to-a-page (the /en portion is being stripped out).
I would like to add a mod_rewrite rule to give an HTTP 301 Redirect response for any incoming URLs with the /en portion in the URL so they are directed to the correct page.
I've tried adding the following lines to my .htaccess file both above and below the existing rules, but in both cases visiting a page with /en results in an HTTP 404 Not Found response:
RewriteRule ^en/(.+)$ http://www.example.com/$1 [R=301]
If I comment out the existing rules, my rule works just fine. I've also tried to add a condition to the rule, but this doesn't appear to have an effect either:
RewriteCond %{REQUEST_URI} =/en/*
This came up for me when writing all of my custom redirects, and it turns out the solution was to add an "L" to the redirect line. Give the following at try:
RewriteRule ^en/(.+)$ http://www.example.com/$1 [L,R=301]
Note the "L" near the end of the line. That, according to the Apache RewriteRule docs, means "Stop the rewriting process here and don't apply any more rewrite rules".
In addition to what sillygwailo suggest, I'd recommend you to make sure that your RewriteCond (needed, I think) actually matches..
from the apache docs:
=CondPattern' (lexicographically equal)
Treats the CondPattern as a plain string and compares it lexicographically to TestString. True if TestString is lexicographically equal to CondPattern (the two strings are exactly equal, character for character). If CondPattern is "" (two quotation marks) this compares TestString to the empty string.
So, It could possibly match only an URL containing an actual '*'..? Not sure, but you could also try this:
RewriteCond %{REQUEST_URI} ^/en/.*

Why is Apache mod_rewrite not behaving as expected

I want to redirect URLs from an old site that used raw URL requests to my new site which I have implemented in CodeIgniter. I simply want to redirect them to my index page. I also would like to get rid of "index.php" in my URLs so that my URLs can be as simple as example.com/this/that. So, this is the .htaccess file I have created:
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond $1 ^assets
RewriteRule ^(.*)$ example/production/$1
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ index.php? [R=301]
RewriteCond $1 !^(index\.php|example|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
It should also be noted that my index.php is actually a symlink to example/production/index.php.
Now, the first rule works as expected - all my styles and images show up just fine, it's the second two rules I'm having trouble with. The second rule is basically to destroy the query string and redirect to my index page (externally). So, I found this in the Apache manual:
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
However, when I try to access one of the old pages, instead of redirecting to my index page, I get a 404 page not found error. I have figured out a workaround by making it an internal redirect, but I would really like it to be external.
The next problem, and the one that has been baffling me the most is with the third rule. I would expect this to do something like the following. If I type in:
http://example.com/this/thing
I would expect it to re-route to
http://example.com/index.php/this/thing
Unfortunately, this does not work. Instead, no matter what I type in, it always routes to my index page as if nothing else was in the URL (it just goes to http://example.com/).
Furthermore, and even more confusing to me, if I replace that rule with the following:
RewriteCond $1 !^(index\.php|example|robots\.txt)
RewriteRule ^(.*)$ index.php/this/thing
If I type in a URL such as http://example.com/other/thing, then it will go to http://example.com/index.php/this/thing as expected, BUT if I type in http://example.com/this/thing it goes to http://example.com/ (my index page). I can't make heads or tails out of it. Any help would be greatly appreciated.
This should solve your index.php problem and it will simply detect if a robots.txt is available:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
hmmm - this doesn't seem to work either. The problem is my URLs aren't really asking for a filename or directory anyway. For example: example.com/index.php/this/thing should call the 'thing' method of the 'this' controller. – Steven Oxley
The condition is: If request is NOT a file and NOT a directory, so that was right, what you should have done is combine the appending of the request string:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]