Triming URL with .htaccess - apache

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]

Related

Whats the matter with this rewrite rule?

I just wanted to try mod_rewrite and read some stuff about it.
I have this file on localhost:
board.php?b=123XYZ
And want to archive:
board/123XYZ
This is what I've got so far: (.htaccess is readable and within the root)
RewriteEngine On
RewriteRule ^board/([A-Za-z0-9-]+)/?$ board.php?b=$1 [NC,L]
But this won't work. I don't understand why as the regex matches.
Please try this:
RewriteRule ^board\/([aA-zZ0-9-]+)\/?$ board.php?b=$1 [NC,L]
I think the forward slashes need to be escaped forward slashes.
This tool helps with regular expression testing:
http://regexr.com/
Try this as,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^board/([A-Za-z0-9-]+)/?$ board.php?b=$1 [QSA,NC,L]
You might supposedly getting 404 not found because Apache is assuming incoming url as directory.

Can somebody explain exactly what this .htaccess RewriteRule does?

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.

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]

Using .htaccess to remove unwanted parts of a URL

To someone who knows what they're doing - tho will be really easy. all I want to do is remove a section of my urls, i assume that this is the easiest way.
This is how my URLs currently look:
/blog/?action=viewArticle&url=$postTile
I want to remove:
?action=viewArticle&url=
So that I end up with something like:
/blog/$postTitle
I've tried the below, but I've had no joy:
RewriteEngine on
RewriteRule ^/bwc/(blog)/(.*)/$ /bwc/blog/?action=viewArticle&url=$2
Please help - I think I need to utilise MOD_REWRITE, but I'm not too sure how.
I am not that sure but you please give a try with this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*)$ http://your_domain.com/blog/DateTimestamps-of-news-posts$1 [R=301,L]
See what comes with... :)
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /site/blog/\?action=viewArticle&url=([^&\ ]+)
RewriteRule ^ /site/blog/%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^site/blog/([^/])+$ /site/blog/?action=viewArticle&url=$1 [L,QSA]
These rules need to be in the htaccess file in your document root.

Help with mod_rewrite rule for dynamic url

Ugh.. mod_rewrite makes me feel stupid. I just haven't wrapped my brain around it yet. :/
I have this url:
http://example.com/a/name/
...that I want to point here:
http://example.com/a/index.php?id=name
...where name is what is getting passed to index.php as the id argument.
Anything I've tried results in either a 404 or a 500.. :(
If you want the trailing slash to be optional, you have to exclude the file you are rewriting the request to. Otherwise you will have a nice infinite recursion.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]
Here any request that starts with /a/… but it not /a/index.php is rewritten to /a/index.php.
But if the trailing slash is mandatory, there is no need to exclude the destination file:
RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]
To start you off:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]
If one rewrite tutorial doesn't work for you, try another.
Edit: excluded index.php as per Gumbo's suggestion
Maybe something along the lines of
RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]
would do the trick.
I suggest you take a look at this URL:
http://www.dracos.co.uk/code/apache-rewrite-problem/
The presented solutions will work, but there are some caveats explained in the URL, mainly regarding ? and # in the URLs themselves.