How come this one works:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/book/blabla$
RewriteRule ^.+$ /book/?name=blabla [NC,L]
But this one doesn't?
RewriteEngine On
RewriteRule ^/book/blabla$ /book/?name=blabla [NC,L]
I've tried many things but it's confusing me.
If you’re using mod_rewrite in a .htaccess file, the contextual per-directory prefix of the URL path is removed before testing the rules:
As you will see below, RewriteRule can be used in per-directory config files (.htaccess). In such a case, it will act locally, stripping the local directory prefix before processing, and applying rewrite rules only to the remainder.
That means if you use mod_rewrite in the .htaccess file in the root directory (/), that path prefix is removed from the URL path. So /book/blabla is reduced to book/blabla. Your rule pattern must reflect that behavior:
RewriteRule ^book/blabla$ /book/?name=blabla [NC,L]
Related
On Apache 2.4 I have an .htaccess (in a subfolder) which rewrites slashless requests inside that folder to appropriate index files:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule (.*) $1/index.html [L]
This works for the slashless version exactly as expected. Now I want to redirect the slashed version externally to the slashless version. I tried adding the lines:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/ $1 [R=302,L]
However this does not work: The redirect is issued, however it does not go to the slashless URL, but to a URL with a system specific part injected.
So, for a sample URL http://example.com/path/to/dir/ the redirected URL looks like this http://example.com/fs9e/username/sub/public/path/to/dir instead of just http://example.com/path/to/dir.
How can I fix this? Many thanks for any pointers!
PS: The real case is a little bit more complicated because I do a subdomain-to-folder rewrite in the root .htacces, but I assume this is not relevant here.
RewriteRule ^(.*)/ $1 [R=302,L]
You are missing the slash prefix (/) on the substitution string (2nd argument) - to make the substitution root-relative. Or rather, /subfolder/ (since this .htaccess file is located in a subfolder). Since this is a relative substitution string (not starting with a slash or scheme+hostname), the directory-prefix*1 (which I assume is /fs9e/username/sub/public/path/) is added back (by default*2), resulting in a malformed redirect. (This is correct for internal rewrites, but not external redirects.)
It should be like this:
RewriteRule ^(.*)/$ /subfolder/$1 [R=302,L]
Note you were also missing the end-of-string anchor ($) on the RewriteRule pattern. (This also negates the need for the preceding condition that checks that REQUEST_URI ends in a slash.)
Note also that this "redirect" should go before the earlier "rewrite".
*1 The directory-prefix is the absolute filesystem path of the location of the .htaccess file.
*2 The alternative is to set a RewriteBase /subfolder - but that then affects all relative substitutions. You could also use an environment variable to apply a specific prefix only to some rules.
I am trying to implement clean URL with the help of .htaccess in my project.
I got a 404 when i implimented a condition with multiple condition strings like keyword1/keyword2/param
all other conditions like RewriteRule ^home index.php [L,NC] works fine
My file structure be like
/subdirectory/
|-.htaccess
|-index.php
|-edit-user.php
|-new-user.php
my desired clean url is
mysite.com/subdirectory/user/edit/10
and it should translated into
mysite.com/subdirectory/edit-user.php?id=10
Some of the closest solutions i tried so far (but no luck)
RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]
RewriteBase /user/
RewriteRule ^user\/edit\/([0-9]+)$ edit-user.php?id=$1 [L,NC]
Any suggestions are highly appreciated.
RewriteRule (.*)/user/edit/([0-9]+)$ edit-user?id=$1 [L,NC]
Since the .htaccess file is inside the /subdirectory then you would need to write the directive like this:
RewriteRule ^user/edit/(\d+)$ edit-user.php?id=$1 [L]
And remove any RewriteBase directive.
\d is simply a shorthand character class for [0-9].
The RewriteRule pattern matches against the relative URL-path (no slash prefix). That is relative to the directory that contains the .htaccess file. You were also missing the .php extension on the filename you are rewriting to. You do not need the NC flag unless you really do want to allowed a mixed-case request, but that opens you up to potential "duplicate content" which would need to be resolved in other ways.
RewriteBase /user/
RewriteRule ^user\/edit\/([0-9]+)$ edit-user.php?id=$1 [L,NC]
Actually, you are very close here, but the RewriteBase directive would have caused this to fail. The sole purpose of the RewriteBase directive is to override the directory-prefix that is added back on relative path substitutions. The RewriteBase directive sets the "URL-path" (as opposed to filesystem path) that is added back.
So, in this example, RewriteBase /user/ would result in the request being rewritten to /user/edit-user.php?id=10 (relative to the root), which is clearly wrong based on the file structure you posted.
Without the RewriteBase defined then the directory-prefix is added back, which results in the rewrite being relative to the directory containing the .htaccess file.
Also, there's no need to backslash-escape slashes since there are no slash delimiters to the regex. (The spaces that surround the argument are the delimiters.)
all other conditions like RewriteRule ^home index.php [L,NC] works fine
Careful with this, as this will also match /homeanything and /home/something etc.
Finally found the issue.
My .htaccess was
RewriteRule ^home index.php [L,NC] RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteRule ^user/edit/(\d+)$ edit-user?id=$1 [L]
(1st line to add .php to anything that comes in, and the 2nd line to convert the desired URL I needed)
What happened here is, when I try to access the URL mysite.com/subdirectory/user/edit/10
The first rule converts that into mysite.com/subdirectory/user/edit/10.php instead of mysite.com/subdirectory/edit-user.php?id=10
This causes the 404 error.
Now I changed the order and the new .htaccess file looks like,
RewriteRule ^admin/edit/(\d+)$ edit-admin.php?aid=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC]
So, when a URL comes in, it will check into all other rules before its matches against the last rule(which appends .php) and translate into the desired result.
Lesson learned: Order matters a lot in .htaccess
I have a problem with redirect. Right now I have one page on address like:
http://localhost/Stella/Wiki/index.php
Also in the same directory I have my .htaccess file, which should redirect all requests to the index.php.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/Stella/Wiki/index.php
RewriteRule (.*)$ /Stella/Wiki/index.php?id=$1 [L,QSA]
This redirection works really good, but I want to use it in relative way, like:
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*)$ /index.php?id=$1 [L,QSA]
Like without absolute path, because I am going to have many subfolders, and I don't want to have long rules like: /xxx/xxx/xxx/xxx/index.php.
Can you help me to solve this problem? I don't know what to do, or if is it even possible?
RewriteCond %{REQUEST_URI} !^/Stella/Wiki/index.php
RewriteRule (.*)$ /Stella/Wiki/index.php?id=$1 [L,QSA]
If the .htaccess is located at /Stella/Wiki/.htaccess then you can write these directives like:
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) index.php?id=$1 [L,QSA]
As you suggested, this now uses relative paths. Note that relative paths don't start with a slash. If you use a slash prefix then that will be root-relative (ie. relative to the document root of the site).
When you use a relative path in per-directory .htaccess files then the directory-prefix (the filesystem path of where the .htaccess file is located) is added back at the end. So, index.php is in the directory where the .htaccess file is located. You can override this with the RewriteBase directive.
However, this a little different to your directives. Instead of a condition that only processes the directive if we are not already requesting the target URL. We have an exception that prevents any further directives being processed if that URL is already being requested.
Note, however, that this directs all requests to index.php (as your original directive does). Including requests for existing files and directories - if that is a concern?
I've a domain that contains a subfolder with the web app structure. I added a .htaccess on my root domain to point the public folder on my subfolder web app. It works fine, but when I type www.example.com the browser URL changes to www.example.com/subfolder/public, but I would like that it doesn't change.
This is my .htaccess:
RewriteEngine On
RewriteRule ^.*$ subfolder/public [NC,L]
EDIT
This first .htaccess is used to redirect on subfolder/public, where there is an other .htaccess that makes all the works.
Here the code of the second .htaccess located on www.example.com/subfolder/public/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Sorry, just realised what is happening. It has nothing to do with the second .htaccess file in the subdirectory, as mentioned in comments.
RewriteRule ^.*$ subfolder/public [NC,L]
Since public is a physical directory on the file system, you need to include a trailing slash when internally rewriting to that directory. Otherwise, mod_dir is going to try to "fix" the URL by appending a slash - that is where the external redirect is coming from. (mod_dir implicitly triggers an external redirect from subfolder/public to subfolder/public/.)
So, try the following instead in your root .htaccess file:
RewriteRule .* subfolder/public/ [L]
The important thing is the trailing slash. The anchors (^ and $) on the RewriteRule pattern are not required, since you are matching everything. And the NC flag is also not required for the same reason.
As always, make sure the browser cache is clear before testing.
UPDATE#1: The single directive above rewrites everything, including static resources, to the directory subfolder/public/ which then relies on the second .htaccess file in the subdirectory to correctly route the request. In order to allow static resources to be rewritten correctly (represented in the HTML as root-relative URL-paths, of the form "/js/myjs.js") then you will need additional directives in order to rewrite these.
For example, to specifically rewrite all .js and .css files to the real location in /subfolder/public/...
# Rewrite static resources
RewriteRule (.+\.(?:js|css))$ subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
UPDATE#2: To make the above more general, and to rewrite any static resource (images, PDFs, .txt, etc...) we can check for the existence of the file before rewriting, something like:
# Rewrite static resources
RewriteCond %{DOCUMENT_ROOT}/subfolder/public/$1 -f
RewriteRule (.+) subfolder/public/$1 [L]
# Rewrite everything else to the "public" directory
RewriteRule .* subfolder/public/ [L]
This will mean that if any .css does not exist it will be passed through to subfolder/public/.
I found a way to redirect (not load, but change the URL) "/" to "/home.html". And now I want to add a RewriteCond to avoid the redirection if the file "/index.html" exists.
I tried (without the comments), but it didn't worked :
# We check that we comes from "domain.tld/"
RewriteCond %{REQUEST_URI} =/
# We check that there is no index.html file at the site's root
RewriteCond %{REQUEST_URI}index\.html !-f
# We redirect to home.html
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]
Help me Obi-wan Kenobi... You're my only hope!
#Gumbo
It's a little bit more complicated than the above example. In fact, I manage both localhost and production development with the same .htaccess, so I tried something like this (following your answer) :
# Redirect domain.tld/ to domain.tld/home.html (only if domain.tld/index.html does not exists)
RewriteCond %{DOCUMENT_ROOT}index\.html !-f [OR]
RewriteCond %{DOCUMENT_ROOT}domain.tld/www/index\.html !-f
RewriteRule ^$ %{REQUEST_URI}home\.html [R=301,L]
I looked at the path returned by "%{DOCUMENT_ROOT}domain.tld/www/index.html" and it's exactly the path of my index.html file... nevertheless, it didn't worked too. :(
By the way, thanks for the "^$" astuce to avoid "%{REQUEST_URI} =/" ! \o/
Any idea why ?
The file check -f requires a valid file system path. But %{REQUEST_URI}index\.html is not a file system path but a URI path. You can either use -F instead to check the existence via a subrequest. Or use DOCUMENT_ROOT to build a valid file system path:
RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]
Furthermore, the other condition can be accomplished with the pattern of RewriteRule. As you’re using mod_rewrite in a .htaccess file, the corresponding path prefix is stripped (in case of the document root directory: /) so that the remaining path is an empty string (matched by ^$).
if you have access to httpd.conf (apaches config file) you could set the default page in there.
Something like this:
<IfModule dir_module>
DirectoryIndex index.html home.html
</IfModule>
Based on the rule set that you posted in your update, you have a bit of a logical error going on. Right now, one of your RewriteCond conditions will always be true, since it seems likely that both index files will never exist in the same environment (one exists in development, the other in production). Since you've OR'ed them together, this means that your RewriteRule will never be ignored due to the condition block.
It's simple enough to fix (I've also added additional forward slashes, since DOCUMENT_ROOT typically doesn't have a trailing slash):
RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteCond %{DOCUMENT_ROOT}/domain.tld/www/index.html !-f
RewriteRule ^$ %{REQUEST_URI}home.html [R=301,L]
Note too that you could setup a virtual host with a local host name so that your development and production would be similar in terms of relative paths.