On Apache webserver, I would like to make a typed URL folder to match another, and convert the end of URL string to a query string for matched folder.
The typed URL can be:
http://www.website.net/corp/view/folder1/folder2
And the resulting viewed page would internally be:
http://www.website.net/corp/files/?dir=folder1/folder2
I tried the following rule in httpd.conf:
RewriteRule ^/corp/view/(.*) /corp/files/?dir=$1 [L], and I get a infinite loop, has the URL is self redirected...
Thanks for your help.
Use .+ instead of .*:
RewriteRule ^/corp/view/(.+) /corp/files/?dir=$1 [L]
.* causes an infinite recursion as it also matches the empty string at the end of /corp/files/.
Related
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.
As part of Google's approach to crawling AJAX-populated content, I've got the following in a .htaccess file:
RewriteEngine on
#snapshot requests
RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]+)
RewriteRule ^.*$ system/snapshot_mode.php?project=%1
I trigger the rule with a URL like
http://myserver.co.uk/?_escaped_fragment_=token
...but it fails to go to the page specified. It does, however, if I remove the query string part of the redirect, i.e. change it to
RewriteRule ^.*$ system/snapshot_mode.php
What's up with that?
[EDIT]
I added a [R-302] to the rule and it now tries to redirect me to the script BUT via a mangled, local machine filepath, i.e.
http://localhost/C:/xampp/htdocs/docula/system/snapshot_mode.php?project=token
...and gives me a 403 access denied error.
Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} _escaped_fragment_=([^&]+)
RewriteRule ^.*$ system/snapshot_mode.php?project=%1 [L,QSA]
RewriteRules and Query String:
By default, a RewriteRule does not change the query string. Therefore, QueryString is not required in the RewriteRule. If you are not adding a parameter to the replacement URL, RewriteRule passes on the incoming query string unchanged.
If you are adding new parameters, the original query string will be discarded and the rewritten URL will get only the new parameters.
If you want to combine the original query string and the new parameters, you can append it using the Query String Append [QSA] flag.
In your code, you have a new parameter project=%1 without the [QSA] flag and therefore the _escaped_fragment_=([^&]+) parameter is discarded, making the RewriteCondition fail.
Also Note:
[R-302] is incorrect syntax. It should be [R=302]
[QSD] - Query String Discard flag is not something we would want in this case.
Am trying to mod_rewrite a URL, but unfortunately without any luck.
http://mywebsite.com/gallery/mycustom-gallery/linkid417
Should be changed to:
http://mywebsite.com/gallery/mycustom-gallery/#417
Where '417' is a dynamic id of an image & mycustom-gallery will also change every-time.
I've tried the following rules, but none seems to work...
RewriteRule ^/gallery/$1/#([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/y/([0-9]+)$ /gallery/$1/linkid [L,R=301]
RewriteRule mycustom-gallery/#.*$ /mycustom-gallery/linkid=417/$1
Regards,
Charl
If you are trying to rewrite from http://mywebsite.com/gallery/mycustom-gallery/linkid417 to http://mywebsite.com/gallery/mycustom-gallery/#417 you can do it as follows:
RewriteRule ^/gallery/([a-zA-Z0-9_-]+)/linkid([0-9]+)$ /gallery/$1/#$2 [NE,L,R=301]
The NE|noescape flag prevents Apache from escaping the hash in the URL during the 301 redirect.
If however you wish to do the opposite, i.e. redirect from http://mywebsite.com/gallery/mycustom-gallery/#417 to http://mywebsite.com/gallery/mycustom-gallery/linkid417, that cannot be done, as the fragment part of the request ("#417") is not passed to Apache along with the request as per RFC1808.
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.
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.