mod_rewrite on 1and1 shared hosting - apache

I'm trying to do some URL rewriting, but my host (1and1) and my little experience with .htaccess files give me troubles.
Specifically, I am trying to setup the .htaccess file on /news directory, where I have a show.php file. My purpose is to convert www.example.com/news/blahblah to www.example.com/news/show.php?article=blahblah.
My .htaccess file so far is (EDIT: updated my .htaccess, still no solution):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/news/([a-z0-9_-]+)$ /news/show.php?article=$1 [L,NC]
</IfModule>
I have messaged my host for details on how to enable the module, and they replied that I should input these lines to enable the module.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
I tried with RewriteBase, and adding the rest lines to the file, but I got no results so far. I am new to .htaccess coding, so I don't know much about these commands, and tutorials don't seem to answer my questions, so any help understanding what I'm doing wrong is dearly appreciated.
EDIT: I updated the .htaccess I have on the folder. I also uploaded the support's file on the root folder, but no result. I still get 404 Not Found errors.

The lines that you've added aren't required. It seems maybe a couple of things may be the issue. Either mod_rewrite isn't loaded, thus the <IfModule> block is never executed. Or the htaccess file is being ignored.
You should be able to tell if you remove the <IfModule mod_rewrite.c> and </IfModule> lines, then try going to a page. If you get a 500 Server error, that means mod_rewrite isn't loaded and the server is puking because the rewrite statements aren't recognized. Otherwise, you can try adding some jibberish into the htaccess file and if you get no errors at all, then it would seem the htaccess file is being ignored.
Seeing as you're on shared hosting you'll need to contact 1and1 support. I have no idea how they run their shared hosting, but if the module isn't loaded, then they need to load the rewrite module, something like:
LoadModule rewrite_module modules/mod_rewrite.so
otherwise they need to make your document root overridable:
AllowOverride All
so that the htaccess file can rewrite the URI.
EDIT:
got to mention that I have no .htaccess on the root folder, just the /news folder.
This is what the problem is then. Your rule needs to be changed to:
RewriteBase /news/
RewriteRule ^([a-z0-9_-]+)$ show.php?article=$1 [L,NC]

Found the solution. I uploaded an .htaccess file at root folder containing only what support gave me, and also uploaded my own .htaccess file at the news folder, and it worked. The correct .htaccess of the news folder is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteRule ^([a-z0-9_-]+)$ show.php?article=$1 [L,NC]
</IfModule>

Related

htaccess - Script inside subdirectory

I have already one blog running in root folder and I want to test another one in a sub directory called "test".
How do I make all links point to example.com/test/login.php and not example.com/login.php ? I don't want to edit all links in my files, I want this one behave as root so later no need to change anything when I put it in production.
I assume it can be fixed with .htaccess crazy Rewrite Module but I haven't figured it out yet so please help save my time!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} /test/ [NC]
RewriteRule !^test/ /test%{REQUEST_URI} [L,NC,R]
Reference: Apache mod_rewrite Introduction

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

redirecting (.htaccess) is not working (NOT RELEVANT ANYMORE)

My .htaccess file is not working.
It looks like the mod_rewrite module is not loaded.
One way to check this to run <?php phpinfo() ?>.
But when I do this the phpinfo pages appeart, but there's no part with 'apache2handler'.
So I can't check if the mod_rewrite module is loaded. I can't see which apache config file is loaded.
Is there anyone who can help me? I'm stuck with this problem for hours now.
This is my .htaccess
RewriteEngine On
#exclusions
RewriteCond %{REQUEST_URI} !images/
RewriteCond %{REQUEST_URI} !external/
RewriteCond %{REQUEST_URI} !css/
#redirect everything else
RewriteRule ^(.*) index.php
Options -Indexes
It is placed in a subfolder of a website, like www.example.com/subfolder/
This .thaccess file works on localhost and on an other website.
This doenst result in an 500 error.
<IfModule mod_rewrite.c>
if mod_rewrite is enabled, you'll get 500 error on the server
</IfModule>
EDIT
I discoverd that here is no apache running on the server, its IIS....
Thanks for help anyway.
If you doubt that mod_rewrite is not working on your apache, and you can't check it in phpinfo() there are plenty more ways to check it. In case you don't have terminal access to the server, you can check by .htaccess
<IfModule mod_rewrite.c>
if mod_rewrite is enabled, you'll get 500 error on the server
</IfModule>

Apache Mod Rewrite -- fake a folder when infact there is one

I have, let's say, www.website.org/folder/ which inside has the following .htaccess
RewriteEngine On
RewriteRule ^folder/[0-9]+ http://www.website.org/folder/index.php?n=$1 [NC]
inside folder I have many folders like 1234, 4567, etc. The behavior I'm looking for is a rewriting from www.website.org/folder/1234 to www.website.org/folder/index.php?n=1234. However, for some reason the rewriting doesn't occur and I get a Forbidden error (given that you can't access the directory itself).
How can I solve this?
Thank you very much
-- Note: I had to put away Options +FollowSymlinks because I was getting a Option FollowSymLinks not allowed here error from the provider's webserver.
-- Edit 1
Following Jason's post I modified the .htaccess as follows (I still kept it in folder):
RewriteEngine On
RewriteBase /folder/
RewriteRule ^folder/([0-9]+)/?$ /folder/index.php?n=$1 [NC,L]
But it still brings me to the folder, why is this? Thanks!
The way your rule is written, .htaccess should be in your webroot not the folder directory.
Alternatively, you could modify your RewriteBase. However, I'd do the above and use:
RewriteEngine On
RewriteBase /
RewriteRule ^folder/([0-9]+)/?$ /folder/index.php?n=$1 [NC,L]

Why is my .htaccess file redirecting to full server path instead of relative path?

I've never had a problem with cakePHP before, but something's odd about this server and is causing the redirects in the .htaccess files to behave oddly.
CakePHP uses mod_rewrite in .htaccess files to redirect requests to its own webroot folder. The problem is that the redirects are listing the wrong path and causing a 404 error. My CakePHP application, which is stored in the listings directory, has a .htaccess file as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [R=301,L]
RewriteRule (.*) app/webroot/$1 [R=301,L]
</IfModule>
(*note that the R=301 causes an external redirect so we can see what is going on from our end. It should really omit this flag and do the redirect internally, transparent to end-users)
This is supposed to redirect any request from http://hostname.com/~username/listings/ to http://hostname.com/~username/listings/app/webroot/
However, rather than simply adding “app/webroot/” to the end as it is supposed to, it is adding the full server path ( /home/username/public_html/listings/app/webroot/ ) resulting in the final URL http://hostname.com/home/username/public_html/listings/app/webroot/ which is obviously incorrect and triggers a 404 error.
The hosting is on a shared hosting account, so that limits what I can do with the settings. I've never seen this happen before, and I'm thinking it's something wrong from the hosting side of things, but if anyone has some helpful suggestions then I can put them to the hosting company as well.
The solution to your question can be found towards the bottom of this page in the cakephp book:
For many hosting services (GoDaddy, 1and1), your web server is actually being served from a user directory that already uses mod_rewrite. If you are installing CakePHP into a user directory (http://example.com/~username/cakephp/), or any other URL structure that already utilizes mod_rewrite, you'll need to add RewriteBase statements to the .htaccess files CakePHP uses (/.htaccess, /app/.htaccess, /app/webroot/.htaccess).
I've deployed CakePHP from my profile's public_html folder as well. I had to change 3 the same .htaccess files mentioned above. Just add RewriteBase /~username/ to the .htaccess files just after RewriteEngine on!
Try removing .htaccess from main file... It worked for me
It was quite simple (using uolhost shared host):
Edit both .htaccess files:
/webroot/.htaccess
/.htaccess
Add the following line:
RewriteBase /
Here is the whole /webroot/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]