Why won't this mod-rewrite rule work? - apache

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.

Related

htaccess mod_rewrite - rewrite folder and file to parameters

I have the follow folder structure for images:
http://www.localhost/memes/01/blaa.jpg
http://www.localhost/memes/02/blaa2.pg
etc.
Now I want to move the structure but the old one must still be available for PHP file.
So it should be rewritten like:
http://www.localhost/memes/01/blaa.jpg
to:
http://www.localhost/memes/?folder=01&pic=blaa.jpg
or to:
http://www.localhost/memes/?pic=blaa.jpg (ignoring the subfolder of memes)
yes it's www.localhost for what ever reason and I don't mind it so far :D
Could you please try following, written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^memes/([^/]*)/(.*jpg)$ memes/?folder=$1&pic=$2 [NC,L]

Redirect to full URL with htaccess RewriteRule

I want to load images from production website if they don't exist on my local environment, for that, I have this htaccess rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule uploads/(.*)$ http://www.productionserver.com/uploads/$1 [R=302,L,NC]
The problem:
Original URL:
http://localhost:8080/myproject/uploads/image.jpg
Output:
http://localhost:8080/myproject/http://www.productionserver.com/uploads/image.jpg
Desired output:
http://www.productionserver.com/uploads/image.jpg
What must I change in order to achieve that?
The code was correct.
I moved it to the top of my .htaccess file and it worked. Some other rule was interfering with it.
I believe the tool I was using to debug the "Output" was wrong. The tool is: http://htaccess.madewithlove.be
Thanks everyone who replied on the comments.

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.

Url rewriting mod_rewrite

I have the url such as:
page.com/content.php?xname=p&yname=q&zid=1
I want to rewrite this url using apache mod_rewrite into something like:
page.com/p/q/
note there should not be 'zid' parameter in renamed url. I know expressions are passed as GET into the original url.
Is it possible to rename as above. If yes, How to achieve this?
This one works fine for me and will rewrite request for /p/q/ to /content.php?xname=p&yname=q&zid=1.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ content.php?xname=$1&yname=$2&zid=1 [QSA,L]
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
It will not rewrite if requested URL is a real file or folder (I'm sure you do not want to rewrite images or some other pages -- I had to add such condition since I do not know what is your website structure is).
RewriteRule ^content\.php\?xname=(p)&yname=(q)&zid=1$ /$1/$2 [R]
Instead of p and q you can try expressions like [a-Z0-9_-]+ to match identifiers.
There's an online testing tool here: http://civilolydnad.se/projects/rewriterule/
RewriteEngine On
RewriteRule ^/[a-z0-9]+/[a-z0-9]+/$ content.php?xname=$1&yname=$2 [L]

.htaccess not working

RewriteEngine on
RewriteCond $1 !^(index\.php|files|assets|robots\.txt)
RewriteRule ^(.*)$ ./index.php/$1 [L]
The website is hosted on the latest version of XAMPP locally. When I load the website with the .htaccess file in place, it won't load at all. I get a server error
What am I doing wrong?
EDIT: Checked log file, here's an error that might help point the issue out. Does this mean that mod_rewrite has not been included?
.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or
defined by a module not included in the server configuration
Change your first line to
RewriteEngine On
Also, replace your RewriteRule with
RewriteRule ^(.*)$ index.php/$1 [L]
If this doesn't help, check your log file. Server errors will normally issue something to the log file.
If that doesn't help, you'd best crank up your RewriteLog and post some of that here.
The first line, as has been pointed out, should be changed :
RewriteEngine On
but you really ought to redirect requests only if they do not already exists (otherwise all your static requests, eg images and css, would go through index.php as well)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you made sure to enable mod_rewrite in XAMPP? A quick googling revealed that (at least in older versions of XAMPP), mod_rewrite was not enabled by default. Maybe this is your issue?