1and1 htaccess errors - .htaccess

The commented out google redirect is working but the rest of the rules dont work at all.
it automatically gives me a 500 internal server error. This only happens on the 1and1 server.
RewriteEngine On
Options FollowSymLinks
# RewriteRule ^(.*)$ http://www.google.com/$1 [r=301,nc]
RewriteRule ^(ajax)/([a-zA-Z0-9-z\-]+)$ http://mysite.com/index.php?ajax=$2 [r=301,nc]
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)$ http://mysite.com/index.php?page=$1&subPage=$2
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/$ http://mysite.com/index.php?page=$1&subPage=$2
RewriteRule ^repairs/([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]*)$ http://mysite.com/repairs-engine.php?page=repairs&subPage=$1&pitem=$2
RewriteRule ^([a-zA-Z0-9-z\-]+)$ http://mysite.com/index.php?page=$1
RewriteRule ^([a-zA-Z0-9-z\-]+)/$ http://mysite.com/index.php?page=$1
Any thoughts?
Thanks

For exact error description you should check the Apache's error log.
What this pattern supposed to mean [a-zA-Z0-9-z\-] ?? It is definitely wrong. It should be [a-zA-Z0-9\-] -- I'm pretty sure that this is the reason for error.
NOTES:
If using [NC] flag, then no need to have a-zA-Z -- just a-z will be enough.
If doing rewrite (internal redirect, when URL changes in browser's address bar) and not proper redirect (301, 302 etc), then no need to use full domain name.
In any case, I suggest adding [L] flag to all rules -- it will speed up processing by tiny bit.

have you tried not to escape the last dash in the char class block?
like this:
^([a-zA-Z0-9-]+)$

Related

Remove first part of url

I am quite sure that this is the right way, but it's not working. I'm trying to rewrite this
domain.com/halloo/wp-content/uploads/image.jpg
to
domain.com/wp-content/uploads/image.jpg
using this in the .htaccess
RewriteRule ^/halloo/wp-content/(.*)$ /wp-content/$1 [R=301,L]
I can't figure out why it's not working.
The regex targets for rewrite rules in htaccess files won't start with a /, which means your rule will never match (because there's never a request that starts with /).
Also, your rule takes the request /halloo/wp-content/foo and redirects the browser to /wp-content/foo. If you want to rewrite it internally so that /halloo/wp-content/foo remains in the URL address bar, remove the R=301, part from the flags.
RewriteRule ^halloo/wp-content/(.*)$ /wp-content/$1 [L]
This means you must request domain.com/halloo/wp-content/uploads/image.jpg in the browser. If you actually wanted it the other way around, just swap the "from regex" and the "to URI":
RewriteRule ^wp-content/(.*)$ /halloo/wp-content/$1 [L]
Edit:
To get rid of the /halloo/ from the browser's address bar, you need something like this:
RewriteCond %{THE_REQUEST} \ /+halloo/wp-content([^ \?]+)
RewriteRule ^ /wp-content$%1 [L,R]

Surprising rewriting of URL by htaccess rule

I've zeroed my problem and I've specific question.
With only the following code in the .httaccess why index2.php gets called if I type in my URL as www.mysite.com/url2 ?
RewriteEngine On
RewriteCond %{REQUEST_URI} (.html|.htm|.feed|.pdf|.raw)$ [NC]
RewriteRule (.*) index2.php [L]
I've also tested it at http://www.regextester.com and should not replace it with index2.php:
In the end I want this rule to skip any URL starting with /url2 or /url2/*.
EDIT: I've made screen recording of this problem: http://screenr.com/BBBN
You have this in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.html|.htm|.feed|.pdf|.raw)$ [NC]
RewriteRule (.*) index2.php [L]
What it does? it rewrites anything that ends with html, htm, feed , pdf , raw to index2.php. So, if you are getting results as your URL is ends with those extensions, then there are two possible answers:
There is another rewrite rule in an .htaccess in upper directories (or in server config files) that causes the URL to be rewritten.
Your URL actually ends with those extensions. have in mind, what you enter in your address bar, will be edited and rewritten. For example, if you enter www.mysite.com/url2 in your address bar and that file doesn't exist on server, your server will try to load the proper error document. So, if your error document is /404.html, it will be rewritten to index2.php at the end.
Update:
I think it's the case. create a file named 404.php in your document root. Inside your main .htaccess (in your document root), put this:
ErrorDocument 404 /404.php
delete all other ErrorDocument directives.
inside 404.php , put this:
<?php
echo 'From 404.php file';
?>
Logic behind it:
When you have a weird behavior in mod_rewrite, the best solution in my experience is using rewrite log. to enable rewrite log put this in your virtualhost or other server config directives you may choose:
RewriteLogLevel 9
RewriteLog "logs/RewriteLog.log"
be careful: the code above will enable rewrite log and start logging at highest level possible (logging everything). It will decrease your server speed and the log file will become huge very quickly. Do this only on your dev server.
Explanation: When you try to access www.mysite.com/url2, Apache gives your URL to rewrite module. Rewrite module checks if any of RewriteRules applies to your URL. Because you have one rule and it doesn't apply to your URL, it tries to load the normal file. But this file does not exit. So, Apache will do the next step which is showing the proper error message. When you set a custom error file, Apache will run the test against the new address. For example if error document is /404.html, Apache checks whether your rule applies to /404.html or not. Since it does, it will rewrite it.
The point to remember is apache will do this every time there is change in URL, whether the change is made by rewrite module or not!
The rule you list should work as you expect if this is the only rule. Fact is that theory is fun, but apparently it doesn't work as expected. Please note that . will match ANY CHARACTER. If you want to match the full stop/period character, you'll need to escape it. That's why I use \.(html|htm|feed|pdf|raw)$ instead of (.html|.htm|.feed|.pdf|.raw)$ below.
You can add another RewriteCond that simply doesn't match if the url starts with /url2, like below. This might not be a viable solution if there are lots of urls that shouldn't be matched.
RewriteCond %{REQUEST_URI} !^/url2
RewriteCond %{REQUEST_URI} \.(html|htm|feed|pdf|raw)$ [NC]
RewriteRule (.*) index2.php [L]
To get a better understanding of what is happening you can alter the rule to something like this. Now simply enter the urls you dont want to be matched in the url bar and inspect the url bar after the redirect happens. In the url-parameter you now see what url actually triggered this rule to match. This screencast shows you a similar version working with a sneaky rewriterule that is working away on the url.
#A way of finding out what is -actually- matched
RewriteCond %{REQUEST_URI} \.(html|htm|feed|pdf|raw)$ [NC]
RewriteCond %{REQUEST_URI} !/foo
RewriteRule (.*) /foo?url=$1 [R,L]
You can decide to match the %{THE_REQUEST} variable instead. This will always contain the request itself. If something else is rewriting the url, this variable doesn't change, meaning you can use this to overwrite any changes. Make sure the url won't be matching itself. You would get something like below. An example screencast can be found here.
#If it doesn't end on .html/htm/feed etc, this one won't match
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /.*\.(html|htm|feed|pdf|raw)\ HTTP [NC]
RewriteCond %{REQUEST_URI} !^/index2\.php$
RewriteRule (.*) /index2.php [L]

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

Rewriting an url for a user-defined domain

I'm not entirely sure if this is possible and tried searching but couldn't find the exact answer to my current situation.
I'm building a service which should allow users to point their own domain to the service. (they need to point an A record towards my server ip)
I'm able to catch the domain using the catch all in apache. So I made a Vhost record for this catch all in httpd.conf. So all not-defined hostnames in apache are pointed towards a certain directory.
Now I would like to pass this domain as a parameter to my service. So is it possible to point this.randomdomain.com to www.mywebserviceurl.com/domain/catch/this.randomdomain.com with .htaccess
The address bar should keep the url this.randomdomain.com
Edit:
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
The above is redirecting but firefox trows an error "The page isn't redirecting properly - Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
And the address is changing which I don't want.
thanks!
Exclude that domain from the rule:
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
And if you want the domain too:
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^(.*)$ http://example.com/blogs/string/%{HTTP_HOST}/$1 [R=301]
See here: http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/
The directive %{HTTP_REFERER} should do what you're after.
If you use Gumbo's answer, but change the arguments in the braces, you can silently redirect. Change [R=301] to [L], which means it's the last rule that will be executed.
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [L,P]
Edit: After seeing Gumbo's answer, you'll also need mod_proxy enabled and add the P flag.

Resources