mod rewrite from .php to subdirectory - .htaccess

Id like to forward links like
https://server.com/checkAvailability to https://server.com/api/checkAvailability.php
I tried the following in .htaccess but it did not work
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ ./api/$1.php [L]

Could you please try following, written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)/?$ api/$1.php [L]

Related

.htaccess URL rewriting does not work

I wrote this code into my .htaccess file, but it seems not to work.
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([^/]*)\.html$ /category/test.php?name=$1 [L]
When writing http://example.com/z.html it automatically redirects me to 404 file.
Do you have any suggestions?
Make sure you have mod_rewrite enabled and try your rules this way.
Options -MultiViews +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.html$ /category/test.php?name=$1 [L]

htaccess RewriteCond for subdirectory not working

I have my .htaccess file written as such for my main program in the top directory but also want to exclude a sub-folder where I want to run another program.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond {REQUEST_URI} !=/ecart
RewriteRule ^files/ - [L]
RewriteRule ^(.*)/$ index.php?layers=$1 [L]
</IfModule>
I am trying the RewriteCond but it's not working.
It should go to the ecart/index.php file but still I am being brought to the front domain when I put my address in as www.mydomain.com/ecart
Your code has syntax error with logical errors. {REQUEST_URI} has missing % sign and RewriteCond is applicable to next RewriteRule only. Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/ecart(/.*|)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /index.php?layers=$1 [L,QSA]

.htaccess remove index.php and hide parameter name

I have the following URL
www.site.com/index.php?key=blah
I want to make it like the following using .htaccess
www.site.com/blah
How can i achieve this using .htaccess?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)/?$ /index.php?key=$1 [L,QSA]
it is what i'm using on my website, it works on local too.
Options -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?key=$1 [L]
</IfModule>

How can I simplify my url with .htaccess?

I would like this:
http://www.website.com/test/view
to actually call this:
http://www.website.com/index.php/test/view
I tried an .htaccess file that looks like this but it doesn't seem to work:
RewriteEngine on
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ /index.php/$1 [L]
What am I missing?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
Did you set "RewriteEngine On" At The first line??
I used this once and it worked fine for me:
RewriteEngine On
RewriteRule ^([a-z\-]+)$ /index.php?page=$1 [L]

mod rewrite clean url for search box

I have mod rewrite enabled to remove all page extentions....
This is done in httpd.conf as I am using apache on windows
the setup I have is
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
and this makes domain.com/bookings.html
appear as domain.com/bookings
I have php enabled in html files, so it parses all pages for php
But I have now put a search box on my site, and if I search for "music" it will use the url:
domain.com/search?q=music
I would like my urls to look like:
domain.com/search/music
But also, I would like to be able to type
domain.com/search/abba
And it would load the page "search.html" lets call it "search" and it would add the parameter ?q=abba , but then still look like the above example in the URL bar
I'm sure this can be achieved with mod rewrite but I am not sure how to phrase the expression.
Thanks in advance for any help I receive :)
use the kind of URLs in your html: domain.com/search/music
add this in your .htaccess file in your DocumentRoot.
Options +FollowSymLinks +Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
or add this to your .conf file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
You may just add one simple RewriteRule:
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
So all in all:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
</IfModule>
Oh by the way:
Here's the wiki of serverfault.com
The howto's htaccess official guide
The official mod_rewrite guide
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Resources