Whats the matter with this rewrite rule? - apache

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.

Related

RewriteRule doesn't rewrite correctly

I'm hosting my development site on the localhost server and I used to access it at 127.0.0.1/dev. In the folder I have a .htaccess file containing following information. I'm new with the RewriteEngine and don't get this work.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php?page=$1
When I'm trying to access 127.0.0.1/dev/home, I simply get a message that the page doesn't found. I can't see where rewrite is redirecting me, so I can't debug the problem in easy way. I think that you can see the problem at the first look.
Thanks in advance.
Try this rule in /dev/.htaccess without leading slash in source pattern and target URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$0 [L,QSA]

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

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 Get requests in the URI

I don't know how I would word this, and I can't make sense of the docs for httpd so I was wondering if anyone knew how to do this.
I would like to get
www.example.com/v/12345/yadayada.png
to actually go to
www.example.com/view?key=12345&img=yadayada.png
I've seen this done some websites but I cant find out how to do this.
Thanks
Edit 1:
I tried this, and then entered the following URL:
www.example.com/v/3f210a2c76cb100f4f7fbd7691a9eb967cb7a1a7/10b78802581bfd59f3fe2b447575bdf7.png
When I did this I got the following error:
The requested URL /v/3f210a2c76cb100f4f7fbd7691a9eb967cb7a1a7/10b78802581bfd59f3fe2b447575bdf7.png was not found on this server.
This is my current .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^./]+\.png)$ /i/$1 [L,NC]
RewriteRule ^v/([0-9]+)/([^/.]+\.(png|jpe?g|gif))$ /view.php?k=$1&img=$2 [L]
#JonLin had the right rule, but the rewrite rule you have declared before that one,
RewriteRule ^([^./]+\.png)$ /i/$1 [L,NC]
is applied to the example url you provided. So, with a request like
www.example.com/v/3f210/10b7.png
would get rewritten to
www.example.com/i/v/3f210/10b7.png
It's worth noting the Flags used for your rules
L - Stop the rewriting process immediately and don't apply any more rules
NC - Makes the pattern comparison case-insensitive.
If you were to remove that rule you would get the results you want. You should also determine if that rule is needed and modify it, the flags, and/or rearrange the order of your rules.
RewriteEngine On
RewriteRule ^v/([0-9]+)/([^/.]+\.(png|jpe?g|gif))$ /view.php?k=$1&img=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try these rules in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^v/([0-9a-f]+)/([^/.]+\.(png|jpe?g|gif))$ /view?key=$1&img=$2 [L]

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.