Rewriting of url in .htaccess doesnt work - .htaccess

I have a file in my directory countries.php which will be use by my client page (index.html) to get list of countries. So far it does work when I make the request using this url localhost/api/countries but when I do localhost/api/countries/cuba for example, it still get the list of all countries
but if I do localhost/api/countries?name=cuba it returns the stuff that i need.
Here is my .htaccess code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteRule ^([^/]+)/?$ countries.php?name=$1 [L,NC]
it removes the .php extension but it doesnt rewrite the parameters

Your rule runs only when the request is for an existent file or dir, remove the conditions or try this
RewriteRule (.*) /countries.php?name=$1

Related

.htaccess GET parameter value to index.php

I will like to do this request in the browser:
https://file.domain.com/iums50.js
But Im really doing a request for the page:
https://file.domain.com/get.php?f=iums50.js
I don't want to redirect the user, I will like to put the content of get.php?f=iums50.js in https://file.domain.com/iums50.js, using .htaccess
Try :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /get.php?f=$1 [NC,L]
This will internally redirect the request from :
/foo.bar
to
/get.php?f=foo.bar
-d means this is a directory and -f means this is a file. RewriteCond ition checks to see that the request is not ! for an existent directory or file before rewriting the request to /get.php .

Simple .htaccess rewrite for _get variable

I am working on a site that uses pages with _get variables for example you can get to www.thewebsite.com/users.php?uservar=username by just using www.thewebsite.com/users/username.
The issue I run into is when I also try to add in url rewrites that also cut off file extenuation so aboutus.php becomes about us.
Can I have these two functions in one .htaccess file?
I tried this but it did not seem to work well
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /user.php?page=$1 [L]
You can have both at the same time, but you need the routing rule (the one that targets user.php) to be below the rule that tries to re-attach the extension. So something like this:
# whatever rule you have to re-attach the extension:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
# now your user routing rule, make sure you add the condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /user.php?page=$1 [L]

mod rewrite rule to convert everything after domain to a get query string

I need some help with rewrite rules in htaccess files. I want everything after the domain, after the first slash to be rewritten to get the query string.
If you take a look at mod rewrite everything after domain into get
this is pretty much what I want except that I believe that one of my rules (rewrites files to have a php extension) is interfering with the linked solution.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
What can I do to integrate the linked solution with this rewrite as well?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?query=$1 [L]
English version: If the file or directory does not exist, rewrite to index.php with the query string in $_GET['query'].
For greater flexibility, you could also not pass the request uri into GET with htaccess and just read it directly from $_SERVER['REQUEST_URI'].

mod_rewrite and .htaccess - stop rewrite if script/file exists

This is my htaccess:
RewriteEngine On
# Stop if it's a request to an existing file.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Redirect all requests to the index page
RewriteRule ^([^/]) /index.php [L]
Now this forwards everything to my index.php script! It dosen't stop if a script exists. Anyone have any idea why this isn't working? I've looked everywhere, but I don't really understand mod_rewrite (as much as I thought!).
The problem has come about because I've put in <script> tags which point to .js files in my web directory which are then forwarded to the index.php script. The web developer toolbar tells me this. :) And clicking links to the js files in firefox's view source window also shows the index.php output.
thank you.
This is because after processing a rewrite rule the whole process restarts with the new url. The processing of an url can go thru the rules over and over again each time with the changed URL, until there is no more change (no applying rules found).
You need this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]
Don't think of the rules as a program, they are rules which can overlap and must be as specific as possible with each one.
I guess you should add [OR] after the first condition like this:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]

Using mod_rewrite only if a 404 would have occured

I am in the process of converting a static website into one using a cms.
I have the cms installed in a sub directory of the public directory. To avoid ending up with ugly domain names (http://example.com/cms/) is there an easy way using mod_rewrite to rewrite http://example.com/… to http://example.com/cms/… while ensuring that if the request wouldn't have ended in a 404, there is no redirect.
An example:
/
/cms/index.html
/cms/file.dat
/file.dat
If the user requests /index.html, they should get redirected to /cms/index.html, but if they request /file.dat, they shouldn't get redirected to /cms/file.dat because the file existed at the requested place
EDIT
Thanks for the answers.
You could use the RewriteCond Directive to check whether there is an existing file that correspond to the requested URL, and only rewrite to your CMS if there is none.
Here is a simple example :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
If there is no existing file that correspond to the requested URL, then that request is rewritten to index.php
You might also want to check for symbolic links and / or directories, btw...
For instance, here is a possibility that can be used when setting up a Zend Framework project :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
(Even though it links to ZF, it should be OK for quite many projects)
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^cms/ cms%{REQUEST_URI} [L]

Resources