Mod rewrite for query string - mod-rewrite

Mod Rewrite noob so pardon my ignorance but all I am trying to do is a simple query string removal
from: http://yourwebsite.com/x?section=y
to: http://yourwebsite.com/x/y
I am adding my mod rewrite rules in my .htaccess like this:
ErrorDocument 404 /404
Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]
The problem is that on visiting:
http://yourwebsite.com/x?section=y
My rule writes it back as:
http://yourwebsite.com/x.php/y
That .php in the pretty url is pretty darn ugly and I am struggling to get rid of it.
What is wrong in my mod rewrite rule?

Most likely you are looking for something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\/(.+)$ /$1.php?section=$2 [L]
RewriteCond %{QUERY_STRING} ^section=(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1/%1 [R=301,L,NE]
It makes two rewrites:
a request x/y is internally rewritten to x.php?section=y
a request to x?section=y is redirected to x/y
Note that one is an internal rewrite, whilst the other redirects the browser to show a less 'ugly' url.
One hint: in case you can use the logging feature of apaches rewriting module (RewriteLog and RewriteLogLevel) this will offer you a wealth of detailed information on what is actually going on inside the rewrite process.

Related

Why am I getting a Redirect Loop for the following RewriteRules?

I'm getting a Rewrite Loop error for the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(apps|site).* [NC]
RewriteRule .* http://site.mysite.com/ [R=301,L]
Why is this happening, what am I missing?
I've tested the following URLs, and they seem to redirect ok:
www.mysite.com/
www.mysite.com/apps
www.mysite.com/apps
mysite.com/
mysite.com/apps
mysite.com/apps/about
mysite.com/site
BUT
www.mysite.com/site
does not want to redirect, the rest all redirects to http://site.mysite.com as expected?
It turns out we had a site folder in our public, which somehow got the action before the .htaccess. Not sure why this happened, but upon removing this redundant folder, everything is working 100%

Friendly URL for images

Please suggest me what to put into .htaccess file.
I have PNG, GIF, JPG images on server http://domain.tld/images/anyimage.anyextension
Want to make URLs more friendly like a http://domain.tld/anyimage.anyextension
This I have now. First two strings change links as described. But last string doesn't change it back for server.
RewriteCond %{REQUEST_URI} ^/images/(.+)(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)$ /images/$1 [L]
If I add
RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/image/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /image/$1 [L,NC]
Right after previous query string rule then images don't open. If before there's no problem. What it could be? Do you have an idea how to fix it? The last string RewriteRule ^/?(.+)$ /?$1 [L] cause this conflict
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+) [NC]
RewriteRule ^$ /%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)$ /?$1 [L]
Not sure how that makes it more friendly, aside from it being shorter. You can try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/images/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /images/$1 [L,NC]
Then you can change all of your links from http://domain.tld/images/anyimage.anyextension to http://domain.tld/anyimage.anyextension
The first condition checks to make sure the request is for anyimage.anyextension, as long as the extension is a case-insensitive png, gif, or jpg.
The second condition checks to make sure the requested image actually exists in the /images/ directory.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /images/(.+)(\.gif|\.jpg|\.png) [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]
This redirects the browser to the non-/image/ URL. There's 2 completely different things going on here. One deals with the browser and the other deals with content on the server. See the top part of this answer that explains how they are different.
The rules that you have won't work. First:
RewriteRule ^/(.+)$ /images/$1 [L]
will never match. URI's used to match against the regex of a RewriteRule in htaccess files have the leading slash removed, so no URI is going to start with a /. You need to get rid of it.
Secondly, once you do, you'll get a 500 internal server error because your rules will cause an internal infinite loop. You need to match against %{THE_REQUEST} to ensure that a browser is actually requesting a URL with the /images/ path in it, not what has been internally rewritten.

Apache mod_rewrite friendly URLs with corresponding 301 redirects

The Problem:
Been spinning my wheels and reading up on this one for awhile and looking for some help now. I'm looking to take a group of non-friendly URLs (there are actually more "groups" but this should me for an example):
domainname.com/?section=zebras
domainname.com/?section=monkeys&id=555
and turn them into friendly URLs, as well as do a 301 on the old versions, so that any old bookmarks (and search engines) will still resolve them. The new format I'm looking for would be:
domainname.com/zebras/
domainname.com/monkeys/555
I'm fully intending to write separate RewriteCond/RewriteRule combinations for each of those scenarios, so I don't necessarily need a super-rule that catches all my scenarios. Oh and this is all in .htaccess.
My Progress:
I was originally getting into a redirect loop because I was just doing two RewriteRules back to back - one for the friendly URL and one for the 301 redirect. Came across my favorite way (so far) around the redirect loop which works (for my scenario #1 at least):
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=zebras$ [NC]
RewriteRule ^.*$ http://www.domainname.com/zebras/? [R=301,NC,L]
RewriteRule ^zebras/$ /index\.php?section=zebras [NC,L]
However, I'd like to have something that works for more than just "zebras" (for instance, I'd like it to work for "lions" as well), so I'm trying to make that more generic. What I am trying now looks like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^section=([a-z]+)$ http://www.domainname.com/$1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
However, this doesn't work. I think I have something "not quite right", I just can't tell what it is - there's something I'm missing or formatting incorrectly somewhere. Sorry in advance for the lengthy description, just wanted to be clear.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
Description
Prevents looping:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Prevents trailing slash problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
Handles rewrites with only section=([a-z]+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=302,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
Handles rewrites with only section=([a-z]+)&id=(\d+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
mistake in your rules:
section=([a-z]+) is not available in the URI part. So, RewriteRule ^section=([a-z]+)$ never matched.

Mod rewrite most requests to index.php

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
##RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(js|ico|gif|jpg|png|css)$ [NC]
RewriteCond %{REQUEST_FILENAME} !^index.php [NC]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
## RewriteRule ^/([a-zA-Z_+-]+).php$ index.php?p=%1 [R=301]
## RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [R=301,L]
## RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php?b={REQUEST_FILENAME}
RewriteRule * index.php?p={REQUEST_FILENAME} [R=301,L]
Above you can see my attempts to redirect any request that is not an existing directory, is not index.php and is not a static resource to redirect to index.php?p=resourcename
I am not having any luck. Basically the purpose of this is to redirect static and old urls to new ones as I have just rewritten an old site.
The PHP will handle the redirect logic.
At the moment this code causes an internal server error, I assume because it is caught in a redirect loop. What have I done wrong? My brain is fried after a long day.
Untested, but worth a try :
RewriteRule .* /index.php?p={REQUEST_FILENAME} [R=301,L]
The ".*" part means you want to match 1 or more characters (any of them). And the "/" in front of the "index.php" is probably not mandatory but makes things clearer even if you have the RewriteBase option set to "/" already.
You may also want to add the parameter "QS" between the brackets, to be sure to get the querystring that may be passed with the queries (would be [QS,R=301,L]). Hope this works, and this helps :)
Edit: There's also the "%" in front of "{REQUEST_FILENAME}", as stated by Gumbo.
This code eventually solved my problem.
Thanks for the help, though.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?([-a-zA-Z0-9_+]+).php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [L]
You forgot the % in front of the variable (%{REQUEST_FILENAME}) and an expression that should be repeated zero or more times (* is just a quantifier):
RewriteRule .* index.php?p=%{REQUEST_FILENAME} [R=301,L]

How do I write a custom mod_rewrite .htaccess file for CakePHP routing?

I've rewritten my web app using CakePHP, but now I need to have my old formatted urls redirect to my new url format. I can't seem to add my own custom mod rewrite rule. I've added it above the main cakephp rewrite rule, but I'm getting an infinite redirect loop. I just want http://mysite.com/index.php?action=showstream&nickname=user to redirect to http://mysite.com/user before the cakephp rewrite happens.
EDIT: Ok, so now when the condition is met it's redirecting but it's appending the original query string to the end. I'm assuming that's due to the QSA flag in CakePHP rewrite rules, but I was under the impression the "L" in my rule would stop that from executing...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action\=showstream&nickname\=(.*)$
RewriteRule ^.*$ http://mysite.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When you do a capture inside the RewriteCond line instead of the RewriteRule, you have to reference the capture with %N instead of $N. That is, your RewriteRule line should be:
RewriteRule ^index.php$ /%1 [R=301,L]
Try to test the request line (THE_REQUEST) to see what URI originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php
RewriteCond %{QUERY_STRING} ^action=showstream&nickname=([^&]*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
But maybe it would be easier to do this with PHP.

Resources