Mod_rewrite causing 404 error - php

These are the .htaccess files from two of my localhost sites.
Virtual host 1 (mysite1.com):
# this is the initialization
Options +FollowSymLinks
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^news-07/?$ news_01_06_2007.php [NC,L]
# Handle requests for "news"
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.php$ $1 [nc]
#RewriteRule ^(.*)\.php$ http://mysite1.com/$1 [R,NC]
However, when I try http://www.mysite1.com/testfile.php - it redirects to http://mysite.com/testfile as it should do, except this message is displayed:
Not Found
The requested URL /testfile was not found on this server.
How can I resolve this error message and ensure my .htaccess file works?
I'm managing to understand .htaccess well, with regard to things like blocking spiders etc. but this one is causing me some problems, anyone know what's wrong and how I can prevent this error happening again in the future?
All help appreciated.

I think you might be trying to do it backwards. Try something like this:
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [nc]

Turn on MultiViews with
Options +MultiViews
This will work not only with .php files but with all files the client claims to accept in the request. See here. Note that wrong-but-working configurations in Apache have given a bad name to MultiViews in the past, see here to make sure you have Apache correctly configured.
If you want to use rewrite rules, I'd do this instead (untested):
RewriteCond $0 !\.[a-zA-Z0-9]+$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ $0.php [QSA]
The QSA flag is necessary in order for you not to lose the query string

Related

mod_rewrite in .htaccess causing directories to lead to error 500

I've been modifying my website URLs in order to beautify them a bit, but I'm currently having an issue regarding to the site directories.
For instance, my links look like users/[user_id]/ instead of users.php?p=[user_id]. Everything is working well apart of the directories, as said above.
When I try to access, for instance, to www.mywebsite.com/js/, I'm facing an error 500 from the server, which, hypothetically, means that there is no file/directory with this name, because of the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d line.
Here is the code I've put in my .htaccessfile:
RewriteEngine On
RewriteRule ^users/([0-9]+)/?$ /user.php?p=$1
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/?$1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
# Error Documents Redirect
ErrorDocument 404 http://www.mywebsite.com/notFound/
ErrorDocument 403 http://www.mywebsite.com/forbidden/
ErrorDocument 401 http://www.mywebsite.com/unauthorized/
# Options
Options All -Indexes
Options All +FollowSymLinks
I don't really understand, as I just started working on this for the first time like a few hours ago, and I just start to understand the regex, and path formatting.
I hope you'll be able to help me, and through this, help people facing this issue too.
Regards
add this
RewriteCond %{REQUEST_URI} !^/(css|js)/
below
RewriteRule ^(.*)$ http://www.mywebsite.com/?$1 [L,R=301,NC]
please put this and check
and this after
RewriteEngine On
// having this rule in place we can remove the upper mentioned rule as apache will stop processing other conditions when see this rule
RewriteRule ^(css|jss)($|/) - [L]

.htaccess not working with php includes

I'm trying do use mod_rewrite at my .htaccess but isn't working.
my url is http://gestor.samfbas.com.br/index.php?p=something
it should be http://gestor.samfbas.com.br/something
The file is in a subdirectory (gestor) in my host, where all the files are.
<IfModule mod_rewrite.c>
Options +FollowSymLinks +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
</IfModule>
Try this one (if index.php is in the root folder http://gestor.samfbas.com.br/index.php):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
This should work. Testet it here on my local maschine (No other server redirects or else, just a fresh xampp installation).
It redirect http://gestor.samfbas.com.br/something to http://gestor.samfbas.com.br/index.php?p=something without changing the url in the browser.
And additional to the question in the comment.
This URL part p= should not be known be outside users!
Better use a long var here like sadff34dngn4nil212ugn=, so nobody can call the index.php with parameters directly from outside. You can't prevent that 100% but the redirect parameter p= is only for internal use.
But its just my opinion on that.
Hopefully that helps a little.
Find the right way to rome ;)

Multilanguage and mod_rewrite

I have a multilanguage website. I want the URL's to be like: http://example.com/en/blog_post/2 where blog_post is the name of the file blog_post.php and 2 is value of the parameter id.
I have this .htaccess code now
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(bg|en)/(.*)$ /$2?lang=$1 [L]
RewriteRule ^(bg|en)/(.*)/([^/.]+)$ /$2?lang=$1&id=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
I tried with this line, but it doesn't work:
RewriteRule ^(bg|en)/(.*)/([^/\.]+)$ /$2?lang=$1&id=$3 [L]
Can you help me please :)
I did it. It works with these lines. Thanks to everyone :)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(bg|en)/post/([^/\.]+)$ blog_post.php?lang=$1&id=$2 [L]
RewriteRule ^(bg|en)/(.*)$ $2?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
As mentioned above, the order of these directives is important. The more specific rules should come before the more general rules and this is a key problem with the above. However, the pattern also needs to be changed (made more specific) to prevent other malformed URLs triggering a 500 Internal Server Error and breaking your site. eg. /en/blog_post/2/3 (an additional - erroneous - /something) would still trigger a 500 error in the "fixed" code above.
So, this could be written as:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(bg|en)/([^/.]+)$ /$2?lang=$1
RewriteRule ^(bg|en)/([^/.]+)/([^/.]+)$ /$2?lang=$1&id=$3
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) /$1.php [L]
The generic (.*) pattern has been replaced with ([^/.]+) to only match path segments (excluding a slash). By doing this it also means that the order no longer matters and /en/blog_post/2/3 will simply result in a 404.
I've also removed the L flag on the initial RewriteRule directives, since you need to continue anyway to append the .php extension.
The RewriteRule substitutions should also be kept as root-relative, ie. starting with a slash. (Or you should include the RewriteBase directive.)
I've also added another RewriteCond directive to make sure that <file>.php actually exists before appending the file extension. If you don't do this and <file>.php does not exist then you will get another 500 error.
You could combine the two RewriteRules into one if you don't mind having an empty id= parameter (which presumably your script handles anyway):
RewriteRule ^(bg|en)/([^/.]+)(?:/([^/.]+))?$ /$2?lang=$1&id=$3
This handles both /en/blog_post and /en/blog_post/2 requests.

Problems with Multiviews and redirecting

I recently deactivate the Multiview option in my htaccess file.
It did the job perfectly, but we also use an external application that call a link with the extension file (http://example.com/export_me?mode=csv&id=3)
So the link does not work anymore and i want to use a rewrite rule to correct this error (i do not have access to the application)
RewriteCond %{REQUEST_URI} ^export_me?mode=(.*)&id=(.*)\$
RewriteRule (.*) export_me.php?mode=$1&id=$2 [L]
But i still have a 404 error.
Can anyone help me ?
Thx
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ export_me.php?/$1 [L]
</IfModule>
Why not just simply:
RewriteRule ^export_me$ /export_me.php [L]
The query string will get appended automatically. You can't match against the query string in the %{REQUEST_URI} variable, only the `%{QUERY_STRING} variable.

Can't get mod_rewrite IfModule to work in .htaccess

I'm trying to get my .htaccess file to forward all URLs to a single page with url parameters so that I can handle the page retrievals that way. What I want to happen is this: say the user types in http://mysite.com/users/dan it should forward to the page http://mysite.com/index.php?url=/users/dan.
Similarly, if the user accessed the URL http://mysite.com/random-link it should forward to http://mysite.com/index.php?url=random-link
Here is the code I tried in my .htaccess file, but it just keeps throwing a 500 error at me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond % (REQUEST_FILENAME) !-f
RewriteRule (.*)$ index.php?url=$1 <QSA,L>
</IfModule>
I UPDATED MY CODE TO THIS AND IT STILL THROWS A 500 ERROR
I changed the < and > to [ and ] and I removed the space after the % in the RewriteCond, but it still throws an error.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I'm a novice when it comes to .htaccess, so any help would be greatly appreciated, because I don't know what's causing the server to timeout.
Couple of problems
Rewrite flags go in square-brackets, eg
[QSA,L]
Your RewriteCondition syntax looks incorrect. Try
RewriteCond %{REQUEST_FILENAME} !-f
Just to be on the safe side, anchor your expression to the start of the string
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Lastly, RewriteEngine and subsequent modifiers requires the FileInfo override. Make sure your server config or virtual host <Directory> section for your document root has
AllowOverride FileInfo
Update
Here's a typical rewrite scheme from an MVC project. This will ignore real files, directories and symlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Categories