Apache2 rewrite returns filename extension - mod-rewrite

I really don't understand where I'm doing wrong. I'm trying to apply a rule to
http://localhost/prezzo/account/1
so that it is rewritten as
http://localhost/prezzo/account/test.php?user=1
I'm using UniformServer as WAMP. I placed the .htaccess file in the subfolder I'm working on (prezzo/account/) with the following rule:
RewriteEngine On
RewriteRule (\w+)/?$ test.php?user=$1 [L]
htaccess tester reports that the rule is applied correclty.
But when I go to the URL http://localhost/prezzo/account/1 and test.php is loaded - which contains simply
<?php
echo $_GET['user'];
?>
it returns the string "php" instead of "1".
If I try with
RewriteRule ^prezzo/account/(\w+)/?$ prezzo/account/test.php?user=$1 [L]
I get 404 not found although htaccess tester reports that the rule is applied correctly and the URL is rewritten as
http://localhost/prezzo/account/test.php?user=1
that if I copy/paste in the address bar it works.

But when I go to the URL http://localhost/prezzo/account/1 and test.php is loaded [...] it returns the string "php" instead of "1".
Yes, this is expected with the rule as posted.
This appears to work in the "htaccess tester" because that tool only makes a single pass through the file, which is not how a real server works.
RewriteRule (\w+)/?$ test.php?user=$1 [L]
When you request /prezzo/account/1 then...
The request is rewritten to test.php?user=1
The L flag causes the rewrite engine to start over using the rewritten URL (test.php?user=1) as input to the next round of processing.
The request is rewritten to test.php?user=php since the regex (\w+)/?$ captures the php part of test.php. (The \w shorthand character class excludes dots and the regex is not anchored.)
The L flag causes the rewrite engine to start over using the rewritten URL (test.php?user=php) as input to the next round of processing.
The request is rewritten to test.php?user=php (again).
Since the URL has passed through unchanged the rewriting process stops and the request is finally rewritten to /test.php?user=php.
Solution A - Use the END flag
One solution is to simply use the END flag (Apache 2.4) instead of L to prevent the rewriting engine from "looping". It will stop as soon as the directive is processed. For example:
RewriteRule (\w+)/?$ test.php?user=$1 [END]
Solution B - Make regex more specific
The other solution (or as well as) is to make the regex more specific, so that it doesn't match test.php. ie. Only match the URL format you are expecting.
The regex (\w+)/?$ would seem to be too generic, as it is basically just matching the last group of letters/numbers on the URL-path. If you only want to match digits (a "user-id") then you could make the regex more restrictive and match only digits instead.
You should also anchor the regex at the start, so that it matches a whole path segment, rather than just capturing the last part that matches. In fact, simply anchoring the above regex would have also resolved this, since test.php would have failed to match because \w does not match dots.
For example:
RewriteRule ^(\d+)/?$ test.php?user=$1 [L]
This will match digits only in the last path segment.
If I try with
RewriteRule ^prezzo/account/(\w+)/?$ prezzo/account/test.php?user=$1 [L]
I get 404 not found although htaccess tester reports that the rule is
applied correctly and the URL is rewritten
If the .htaccess file is located in the /prezzo/account subdirectory (as you stated) then the above will never match and the directive does nothing.
That testing tool assumes the .htaccess file is located in the document root only. If your .htaccess was located in the document root and not the subdirectory, then that directive would indeed be OK.
In a directory context (eg. .htaccess) the RewriteRule pattern matches against the URL-path relative to the directory that contains the .htaccess file.
Aside:
In your link, the entire file would seem to be:
RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-l
#RewriteRule . index.php [L]
RewriteRule (\w+)/?$ test.php?user=$1 [L]
The first rule is commented out so does not apply here. However, if you uncomment that first rule then the rules are in the wrong order. Since a request for /prezzo/account/1 would first be rewritten to index.php and you'd have the same problem as before.
The order of rules is important.

Related

mod_rewrite: transform = and & in slashes

I was just looking for a solution to transform any =,?,& found in a query string into a simple slash /.
To be more specific, my link is something like:
http://www.mydomain.com/product.php?c=1&sc=12&products_id=15
and I would it like this:
http://www.mydomain.com/product.php/c/1/sc/12/products_id/15
whatever the master page could be (in this case is product.php, but it could be foo.php, bar.php...or else).
I have googled a lot but didn't find any good solution to achieve what i'm looking for.
I have found complex rewrite rules, but they all include the "page name" into them:
i.e.
RewriteRule ^/?index/([^/]+)/([^/]+)$ /index.php?foo=$1&bar=$2 [L,QSA]
That rule is only applicable to index.php and to known variables like foo, bar.
I need a more general one, whatever the master page is, whatever the variables are.
Can this be done?
Any suggestion?
Thanks
I assume you're using apache >= 2.2. Add this to your apache conf:
<IfModule mod_rewrite.c>
RewriteEngine On
# you absolutely need to use RewriteBase if this snippet is in .htaccess
# if the .htaccess file is located in a subdirectory, use
# RewriteBase /path/to/subdir
RewriteBase /
RewriteCond %{QUERY_STRING} ^(=|&)*([^=&]+)(=|&)?(.*?)=*$
RewriteRule ^(.*)$ $1/%2?%4= [N,NE]
RewriteCond %{QUERY_STRING} ^=$
RewriteRule ^(.*)$ $1? [R,L]
</IfModule>
The first RewriteCond/RewriteRule pair repeatedly matches a token delimited by & or = and adds it to the path. The important flag is the [N] that causes the whole ruleset to start over again, as often as the rule matches. Additionally, a = is appended to the end of the query string. This is to create a mark in the URL that at least one rewrite has happened.
The second ruleset checks for the = mark that remains after the URL has been rewritten completely and issues a redirect.
Have a look at http://wiki.apache.org/httpd/RewriteQueryString for some useful hints.

Need help removing part of filename from url with mod_rewrite

I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.

Problem redirect CodeIgniter URLs to WWW

I have a CI application that uses .htaccess for URL routing. My basic setup is as follow:
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
These rules are pretty standard for CI apps. They rewrite all URLs (except for those in the exception list) to the index.php front controller. The lines above also hide index.php, as it would normally appear as part of every URL.
So far, so good. Everything works just fine. Now, for the sake of SEO I would like to force all traffic to www. So I extended the rules as follow:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc]
These last two lines rewrite http://jungledragon.com/anything URLs to http://www.jungledragon.com/anything URLs. This kind of works, but it brings back the index.php part back: http://jungledragon.com/anything becomes http://www.jungledragon.com/index.php/anything.
How exactly do I combine these rules so that they do not interfere with each other? I tried doing the WWW rewrite before the CI rules. That shows an Apache 301 page with an error, rather than doing the actual redirect.
Additionally, I would like to also include rules to get rid of trailing slashes, but for now let's keep the question simple. Note that I did find useful post here and elsewhere yet for some reason I still can't find the correct exact syntax for my situation.
Edit: Thanks for the help. This works:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc,L]
RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
mod_rewrite processes rules in a linear fashion. Rules at the top of the file are processed first.
The [nc] and [L] at the end of the rules are the options for how to process rules.
nc - nocase: case insensative
L - last: last rule in the execution (if you hit this, stop processing)
You need to put your www redirect rules above your CI rules so it will first add the www, THEN apply the CI rules to the newly re-written url. **And also use either the C or N flag with your www redirect rule so it will parse the next rule.
http://mysite.com/blah ==becomes==> http://www.mysite.com/blah ==becomes==> http://www.mysite.com/index.php/blah (Executed, not redirected)
What's happening currently is:
http://mysite.com/blah ==becomes==> http://mysite.com/index.php/blah (STOP)
Browser goes to http://mysite.com/index.php/blah and a second re-write pass is done since your exceptions stop /index.php urls from being processed
http://mysite.com/index.php/blah ==becomes==> http://www.mysite.com/index.php/blah (Redirected)
As Suggested, here is a link to mod_rewrite's documentation if you want to look further.
#LazyOne: Brainfart, sorry.
Here's an excerpt from the docs outlining the flags you'll probably need:
'chain|C' (chained with next rule)
This flag chains the current rule with the next rule (which itself can be chained with the following rule, and so on). This has the following effect: if a rule matches, then processing continues as usual - the flag has no effect. If the rule does not match, then all following chained rules are skipped. For instance, it can be used to remove the .www'' part, inside a per-directory rule set, when you let an external redirect happen (where the.www'' part should not occur!).
'next|N' (next round)
Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. This corresponds to the Perl next command or the continue command in C. Use this flag to restart the rewriting process - to immediately go to the top of the loop.
Be careful not to create an infinite loop!
'nocase|NC' (no case)
This makes the Pattern case-insensitive, ignoring difference between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
'noescape|NE' (no URI escaping of output)
This flag prevents mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from happening. This allows percent symbols to appear in the output, as in

always preserve URL when redirecting to subfolder

I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like 'http://localhost/app/somepage.htm':
RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app(.*) /app/app_site$1 [L,PT]
This results in the correct page, while preserving the URL. Also, 'http://localhost/app/' will fetch the index page at /app/app_site/index.html, while preserving the URL 'http://localhost/app/'.
However, when I enter 'http://localhost/app', following things happen:
the correct page is fetched, at /app/app_site/index.html
yet, the URL is redirected to 'http://localhost/app/app_site/'
I'm nearly there, but would like to preserve the URL in all cases (also those without trailing slash). Anyone have a clue how to do this? Thanks!
This is the expected behaviour with DirectorySlash enabled, because you've rewritten to a directory that lacks a trailing slash, and mod_dir performs this cleanup after you've rewritten the URL with mod_rewrite.
The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:
RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$ /app/app_site/$1 [L,PT]
This prevents mod_dir from having to add the trailing slash, and therefore avoids the external redirection to /app/app_site/ you're experiencing now.

mod_rewrite issues

I have used the mod_rewrite module but was not able to redirect to the target page - I am getting an error:
The requested URL /old.html was not found on this server.
Rewrite rules as follows:
RewriteEngine On
RewriteRule ^/IN/index.html$ /IN/index.iface [L]
You need to request a URL with a path that’s matched by the pattern of your RewriteRule directive. So in your case obviously /IN/index.html (where the . can actually be any character, as it’s not escaped).

Resources