So I wanted to create a rewrite rule such that when a uses types in
mydomain.com/toplevel/secondlevel/
it rewrites it to
mydomain.com/toplevel/secondlevel
similarly if I have
mydomain.com/toplevel/
I wanted this to be
mydomain.com/toplevel
I understand that this can be done using the rewrite command and regex, but can't seem to find the right regex for it.
Add this rule to main server section:
rewrite ^/(.*)/$ /$1 permanent;
It removes slash at the end from any adress which contains it.
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.
I try to make specific rewrite rules by .htaccess but i need help, its doesn't work.
This is my url :
http://mywebsite.net/aaa-bbbb/cccc
aaa = dynamic parameter ( [a-z] )
bbb = dynamic parameter ( [a-z] )
cccc = dynamic parameter but optionnal ( [a-z] )
And i want rewrite user to this url :
http://mywebsite.net/mydir/ccc?x=aaa&y=bbbb**
url still will http://mywebsite.net/aaa-bbbb/cccc on tabbar
i try this
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ mydir/$3 [L]
but it doesn't work
thx !
To rewrite a request to
http://mywebsite.net/aaa-bbbb/cccc
internally to
/mydir/ccc?x=aaa&y=bbbb
you need such a rewrite rule:
RewriteEngine on
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ /mydir/$3?x=$1&y=$2 [L]
Note: the above rule has the syntax to be used inside the http servers host configuration. If you really want to use a .htaccess style file instead you need to use a slightly modified syntax, since those files work on relative paths:
RewriteEngine on
RewriteRule ^([a-z]+)-([a-z]+)/([a-z]+)$ mydir/$3?x=$1&y=$2 [L]
You can spot the missing / in the rewrite goal. The .htaccess style file has to reside inside the folder holding the mydir folder for this to work. Also the interpretation of such files has to be enabled at all in the http server configuration for.
A general note: .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server for nothing. They are only offered as a last option for those not having access to the http servers host configuration. So for example for cheat shared hosting providers. In general you should always prefer to place such rules inside the host configuration itself.
I want to direct my users based on a case-sensitive url:
www.mysite.com/a ==> page 1
www.mysite.com/A ==> page 2
I'm using the ISAPI rewrite with the following rule:
RewriteRule ^([0-9a-zA-Z] {1,7}) $/redirect/?K=$1 [L]
Apparently this rule is not case-sensitive, since it redirects to the same page. What's is wrong?
=====UPDATE=====
I solved in part this problem by adding a binary query (case-sensitive) in my MySql statement. But in chrome this problem still occurs.
I see 2 problems:
1) space between [] and {}
2) your regular expression is non-case sensitive
for lowercase expression you need RewriteRule ^([0-9a-z]{1,7}) $/redirect/?K=$1 [L]
and for upper-case RewriteRule ^([0-9A-Z]{1,7}) $/redirect/?K=$1 [L]
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.
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/.