Help with mod_rewrite rule for dynamic url - apache

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.

Related

Replace substring in URL via RewriteEngine

im desperately trying to make RewriteEngine to rewrite the following pattern:
https://example.com/api/model/id/
https://example.com/staging/api/model/id/
internally to
https://example.com/index.php/model/id/
https://example.com/staging/index.php/model/id/
I already tried several suggestions from several boards but none of them worked out for me. Ideally the rule should just search for "/api/" and replace it with "/index.php/". I can't figure out, why it is so hard to make that work, my other rules worked out fine till now...
Here is my last try:
RewriteEngine On
RewriteRule ^(.+)/api/(.+)$ $1/index.php/$2 [R=301,L]
# RewriteRule ^/api/(.*)$ /index.php/$1 [R=301,L,NC]
# RewriteRule ^(.+)/api/(.+)$ http://localhost/dev/someFolder/index.php/$2 [R=301,L]
What am I making wrong? I'm just telling the rule to make ($1)/api/($2) to ($1)/index.php/($2), that shouldn't be that hard. Ideally the rule also shouldn't care about whats standing before the "/api/" pattern.
something like this...
RewriteEngine On
RewriteRule ^api/(.*)$ /index.php/$1 [NC]
RewriteRule ^staging/api/(.*)$ /staging/index.php/$1 [NC]
I found a work-around, which seems to work out fine in most use-cases:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) ./index.php/$1 [L]
Found the answer here: htaccess remove index.php from url
Just keep in mind, that you have to put your files in a subdir named after the string you want to replace "index.php" with (e.g. to replace /index.php/$1 with /api/$1 you have to put all your files into a subdir named api).
This is just perfectly fine for my use-case.

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.

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]

.htaccess question - URL-rewriting

I have an issue with URL-rewriting in .htaccess. Here is .htaccess file:
RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-all-results$ forums/index.php?view=view-all-results [R=302]
RewriteRule ^view-all-results/$ forums/index.php?view=view-all-results [R=302]
I need to rewrite url like "/community/view-all-results?u=2" to "community/forums/index.php?view=view-all-results&u=2".
But according to the above rule I'll get "community/forums/index.php?view=view-all-results".
I tried to change RewriteRule to
RewriteRule ^view-all-results?(.*)$ forums/index.php?view=view-all-results&$1 [R=302]
But it doesn't work properly. It still rewrites URL to "community/forums/index.php?view=view-all-results".
When I changed rule(put + instead of *):
RewriteRule ^view-all-results?(.+)$ forums/index.php?view=view-all-results&$1 [R=302]
I've got URL like "community/forums/index.php?view=view-all-results&s". So I don't understand this behavior.((
I will be very appreciated for any suggestions.
The magic flag is in the docs: [QSA], which will add the original querystring to your url.
Normal matching is only done against the path, not agains the querysting, which you would find in the magic variable %{QUERY_STRING}). Matching this variable can be done in a RewriteCond condition. You could also append this variable to the resulting url, but QSA is infinitely more userfriendely here.
Give this a try...
RewriteEngine On
RewriteBase /community/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^view-all-results/?$ forums/index.php?view=view-all-results [QSA]
Basically the first half of a RewriteRule doesn't match against the QUERY_STRING, so you second to example will never match against it. The main thing your first code was missing was the QSA flag, which tells it to pass the QUERY_STRING it receives along with the newly created QUERY_STRING. I also removed the R=302, as I assume you don't want the URL to change.
Edit: Oh, I also combined the rules by making the trailing slash optional.