Can somebody explain exactly what this .htaccess RewriteRule does? - apache

I have the following RewriteRule inside of a .htaccess file and I'm having a hard time trying to figure out exactly what it does. Can somebody please dissect the following and explain what it does? Here is my full .htaccess file:
Options -Indexes
RewriteEngine on
RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]

This rule is trying to add a trailing slash to your URLs. But I must add that there is better way to write this rule.
Replace your rule with this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
Your rule can add a trailing slash to any file without a dot in it. So for example a filename called /abcd will be redirected to /abcd/ and that would cause 404.

Related

Simple .htaccess rewrite rule but doesn't work properly

I've trying to rewrite one url but something doesn't work as must and I get the massage
Not Found
The requested URL /1/1.html was not found on this server.
This is what I have in .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)\.html$ /view.php?id=$1&name=$2 [L]
And this is the href link for this case
href="/'.$row['id'].'/'.$row['name'].'.html"
Any idea why is this?
You must remove leading slash in your rule
RewriteRule ^([^/]+)/([^/]+)\.html$ /view.php?id=$1&name=$2 [L]
You need a leading slash in your rules only if you write it directly in httpd.conf instead of .htaccess files.
You also need it until Apache 2.4 if i don't make a mistake

Unexpected behaviour of rewriterules in htaccess

This is my htaccess file
RewriteEngine on
rewriterule ^([a-zA-Z0-9]+) /vixer.php?v=$1 [QSA]
the main problem is that when I enter www.example.com/vix_25 it goes to 404
and when I enter another url /vix25 it works.. I really dont understand what is the diffrence and why 1st doesnt work.
Can you guys help me? thanks
You need to add an underscore to the pattern so that it can match a "_" in the Requested URI
([a-zA-Z0-9_]+)
Try this :
RewriteEngine on
rewriterule ^([a-zA-Z0-9_]+)$ /vixer.php?v=$1 [QSA,L]
I would make it a bit more robust for your rule. Tell apache that if it's not a real file and not a real directory then rewrite it. This prevents 404's too. I also changed the regex so that it will match better instead of individually adding matches. Also I would make sure MultiViews are off.
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
rewriterule ^([^/]+)/?$ /vixer.php?v=$1 [QSA,L]

Triming URL with .htaccess

I've been reading a lot about rewriting but simply I'cant make it done the right way.
So my question is how can I with .htacces and rewrite rule make this link :
www.website.com/?section=rijecnik
look like this
www.website/rijecnik
Now I'm using this rule but it doesn't work
RewriteEngine On
RewriteRule ^/([A-Za-z0-9]+)$ ?section=$i
Can anyone explain to me what am I doing wrong ?
You have to remove leading slash after RewriteRule.
Your code should look like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /?section=$1 [L]

.htaccess rewrite all 'other' URLs

Hopefully this is a simple one. I have a really basic .htaccess that rewrites any request to /admin (or /admin/etc) to /_admin/index.php. So far so good:
#Options All -Indexes
RewriteEngine On
RewriteBase /admin
RewriteRule ^admin/$ /_admin/index.php [QSA]
RewriteRule ^admin$ /_admin/index.php [QSA]
RewriteRule ^admin/(.+)$ /_admin/index.php [QSA]
What I also want is a generic "catch all else" rule that rewrites any other url (/users, /turnips/, /some/other/path and so forth) back to /index.php
I can't seem to get that to work - its either server error 500's or /admin also gets rewritten to the root page. I'm sure I just need to do something with RewriteCond but I can't figure it out.
Thanks!
Add this after the other rules. It would be the default rule if the previous rules are not applied.
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule .* index.php [L,QSA]
First of all I suggest you add the L flag to your rewrites so you're sure to avoid unintended matches after matching a rewrite (unless intended of course).
Secondly WordPress uses the following code to rewrite all URLs that are not matching index.php OR a file that already exists. This way files accessed directly like images, text files, downloads etc are not rewritten. Note that originally it also included the line RewriteCond %{REQUEST_FILENAME} !-d to also not rewrite directories but you seem to want that behaviour:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php - [L]
Please see if this fits your needs.

Rewrite URLs with .htaccess but ignore specific Directories

I have a directory on a site:
http://example.com/directory/
In it I have a .htaccess file.
I want it to take any URL like this:
http://example.com/directory/section/day/
And rewrite it to:
http://example.com/directory/index.php?arg1=section&arg2=day
Except for any URLs that refer to these directories:
http://example.com/directory/css/
http://example.com/directory/javascript/
http://example.com/directory/images/
I have the first part working, but unable to tell it to exclude files in certain directories:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
Update:
This works in a very basic and simple sense:
RewriteRule ^css - [L,NC]
RewriteRule ^javascript - [L,NC]
RewriteRule ^images - [L,NC]
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
but I'm sure there is a more elegant solution?
I think the keyword you're looking for is RewriteCond. It's pretty similar to what you ended up with.
RewriteCond %{REQUEST_URI} !^(css|javascript|images)
RewriteRule ^([^/]+)/([^/]+) index.php?arg1=$1&arg2=$2 [NC]
You might be able to use RewriteCond to check if an actual file exists... at least, I use this for websites when I want things like CSS, Javascripts, and images to be accessible (without the request being redirected.)
Here's the line I use:
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
Let me know if that works for you!