Changing file name in htaccess gives error 500 - apache

I have set
mydomain.com/page
to point to
mydomain.com/script.cgi
using htaccess and mod_rewrite, which works perfectly. BUT, when I change the file name and the associated rewrite rule I get error 500. Initially I thought it was a browser caching issue, but clearing the cache doesn't help. Here's the htaccess file:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page(.*)?$ /script.cgi$1 [NE,NC,L]
As I said this works perfectly - until I change the file name and the rule. If I change my script file name to script2.cgi and modify the rule to point to script2.cgi, it throws an error. Is it some kind of server caching issue?

I found the answer. It won't be obvious here because I used placeholder names. The problem was my alias folder and destination script began with the same letters.
/page -> /page.cgi
Because I used ^page(.*)?$ in the rule, it was also matching page.cgi thus creating an infinite loop. The answer is to either change the rule to ^page$ to match the entire alias name only, or change the name of the script to something else. Thanks everyone. Hope this helps someone else.

Related

Htaccess access specify file

I have .htaccess file:
DirectoryIndex index.php
RewriteEngine on
RewriteRule ^play/([^/\.]+) index.php?task=view&name=$1 [L]
Show page game.
My problem: When I need to load some file (or access by address bar) with path: /play/Assest/file-name.swf. This return 404 error.
How I can access file but don't change RewriteRule above?
I tried redirect code but it's not working:
RewriteRule ^/play/Assets/file-name.swf ^/games/Assets/file-name.swf [R=301,L]
Your RewriteRule is missing an anchor to the end of the URL, so partial matches still get rewritten. Add a $like this:
RewriteRule ^play/([^/.]+)$ index.php?task=view&name=$1 [L]
Shahaf's answer may also help you (although it means the file system gets polled twice for every request, which affects performance), but with this above you are saying "only match play/ with anything but dots or forward slashes following it" which seems to be what you mean. Without the dollar it can have anything after it and still match, as you have found.
I also removed the escaping of the dot which is not necessary in a character class.
Before the rewrite rule you should add conditions if it's not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

problems getting mod_rewrite working

I've not done much with mod_rewrite, but I can't seem to get anywhere with this. I'm wondering if perhaps it is not enabled on my server(even though my host says it is).
I have the following url: http://dev.website.com/folder1/translate/horse and I want that to redirect to: http://dev.website.com/folder1/translate.php?word=horse
My .htaccess starts with RewriteEngine on and I've tried various attempts to get it working, but no matter what, it just shows my home page (the default 404 redirect).
Things I've tried:
RewriteRule ^translate/.*$ translate.php?word=$1
RewriteRule ^translate translate.php
and some other things I don't remember, but I can't get anything to work.
The .htaccess file I am using is located in folder1. I have also tried putting random characters in the file to make it throw an error, and it does.
Anything I'm missing? How would I properly create this redirect?
As per request, this is my file structure.
I have the domain www.website.com, and a subdomain dev.website.com. The subdomain is set so that it redirects to www.website.com/dev. So, in this case, dev.website.com/folder1/translate.php = www.website.com/dev/folder1/translate.php. I am not sure how that masking is done, as it is accomplished via my web host's cpanel.
You aren't capturing $1 in brackets so this should work:
In DOCUMENT_ROOT/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder1/translate/(.*)$ /folder1/translate.php?word=$1 [L,QSA]
In DOCUMENT_ROOT/folder1/.htaccess:
RewriteEngine On
RewriteBase /folder1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^translate/(.*)$ translate.php?word=$1 [L,QSA]

Mod_rewrite rules from root

This problem has been bugging me for a while now.
I have a created a small site engine and I'm using mod_rewrite to tell the engine what page to proccess, SEO friendly links is a bonus :).
This is how it's works today:
the adress http://www.example.com/site/page
becomes http://www.example.com/engine.php?address=page
But what i want is:
the adress http://www.example.com/page
becomes http://www.example.com/engine.php?address=page
Everything works fine if i create a psuedo directory for the calls (/site) but when i try to do the same from the root strange things start to happends.
RewriteBase /
RewriteRule ^site/(.*) engine.php?%{QUERY_STRING}&address=$1
Works fine: /site/about/contacts becomes eninge.php?address=about/contacts
RewriteBase /
RewriteRule ^(.*)$ eninge.php?%{QUERY_STRING}&address=$1
Doesn't work, for some reason /about/contacts becomes eninge.php?address=eninge.php
(.*) means catch anything. Have you tried exluding files and directory before your catch-all ? Because it will cause an infinite recursion without it.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ eninge.php?%{QUERY_STRING}&address=$1 [L]
More information is available in the official documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Update: You should also specify [L] at the end of your rule, to tell Apache to end the rewriting process here.
Check the RewriteLog (this has been updated in 2.4, check current docs if not using 2.2):
RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3
This will show you exactly what mod_rewrite is doing and allow you to tune your configuration based on its output. Beware - it grows very quickly, and should never be used in production environments.
As an aside, you have some typos in your post - worth verifying that these differ from your config.
RewriteCond %{REQUEST_FILENAME} !^engine.php
RewriteRule (.*) engine.php?address=$1 [QSA,L]
Try this. What you have is causing the rewrite to loop around and first do engine.php?address=about/contacts as you were expecting, but then go around again and rewrite that to engine.php?address=engine.php. Make sense? The [QSA,L] is a Query String Append and Last flag that will add the query string to your URL and tell the rewrite engine to stop looking for rewrites. The RewriteCond %{REQUEST_FILENAME} !^engine.php is to check that you haven't already specified the engine rewrite by ensuring the current URL doesn't start with engine.php. This is necessary if you are writing this in an .htaccess file rather than the .httpd config files.

how to make url rewrite apache whitout any rewrite condition?

sorry, but i'am less understand about url rewrite...
i want to rewrite my url from :
http://localhost/controller/index.php/user/edit
to
http://localhost/controller/user/edit
i can make it with this .htaccess :
RewriteEngine On
RewriteBase /controller/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [PT,L]
but, the rewrite works if there is no file exist at controller/user/edit.php
i want every request to under my controller/xxx is rewrited to controller/index.php/xxx whether the file is exist or not....
i have remove the RewriteCond so my current one is like this :
RewriteEngine On
RewriteBase /controller/
RewriteRule ^(.*)$ index.php/$1 [PT,L]
but, it shown internal service error..
There are a lot of things that don't make sense to me. Mainly, your question says to want to rewrite a URL having index.php in it to one that does not, but your rewrite rule, which you say works in some cases does the opposite, it pre-pends index.php to requests.
If you have access to your apache error and access log, you might see if there's more information about exactly at what point the error occurred -- was it when the .htaccess file was processed, or was it from within your php program?
I will assume that the goal here is to take "pretty" urls like /controller/user/edit and have the index.php program actually process the /user/edit part of the path.
If so, I think you may want to set the RewriteBase to /, and change your .htaccess to
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ controller/index.php/$1 [PT,L]
The RewriteBase / directive says that all requests are relative to the server's DOCUMENT_ROOT setting. The change to the rewrite rule instructs all requests to go to the directory controller and file index.php, appending the original requested path afterwards.
(Note: I don't think you want to use the PT flag in this case, and it would be better form to escape the . which is a regex operator as index\.php, but I think neither of these are relevant to the problem here)
It is not clear if you do want the / before the $1. If your PHP program (index.php) is getting called with it present, and knows how to handle it, then it's fine, but it's a little unusual, and there may be cases where you end up with multiple /'s from within the php program.
But do you really want to do this? The typical use of the RewriteCond %{REQUEST_FILENAME} !-f is to handle cases such as image files and css or javascript files that are static and need not be handled by your controller. RewriteCond %{REQUEST_FILENAME} !-d depends on your system (but it's purpose to see that the request is not for a directory).
Anyway, the basic change as I proposed might help, but if not, perhaps you can clarify your intent and provide some actual URLs and a look inside index.php

Why won't this mod-rewrite rule work?

I have a development site on my machine at
localhost/~Jason/hfh/admin/?admin=collections
My .htaccess file is in the /hfh/admin/ directory. It says:
RewriteEngine On
RewriteBase /~Jason/hfh/
RewriteRule ^([A-Za-z0-9\-\_]*)$ index.php?admin=$1
But when I go to
localhost/~Jason/hfh/admin/collections
I get a "page not found" error. Can anyone tell me why?
(This is related to another question at this link.)
If you have the .htaccess file in /hfh/admin/ make that the base to begin with.
RewriteBase /~Jason/hfh/admin/
then you may see what you expect. Also you'll may want a clause to not redirect when the File/Directory exists.
Does typing the expected result URL work?
/~Jason/hfh/admin/index.php?admin=Collections
Edit:
So what happens if you change the whole lot to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~Jason/hfh/admin/index.php?admin=$1 [L]
It looks like that would be sending you to the page /~Jason/hfh/index.php?admin=collections, when you want /~Jason/hfh/admin/index.php?admin=collections.
Try changing the rule to:
RewriteRule ^([A-Za-z0-9\-\_]*)$ admin/index.php?admin=$1
The short, direct answer for now appears to be: you can't use mod_rewrite on your localhost.