I have configured my server using htaccess so that I can route all URL's into a PHP $_GET parameter. I want the URLs on the site to finish with .htm. Currently I have this htaccess, and I have never had any problems with it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ajax/([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1&opformat=ajax [QSA]
RewriteRule ^([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1 [QSA]
However I have just installed TinyMCE which uses .htm files in the js folder e.g. "/js/tinymce/prop.htm". For some reason when this URL is entered my htaccess routes it to php and it doesn't work.
I was given to understand that RewriteCond %{REQUEST_FILENAME} !-f should stop this from happening. Why is it not? RewriteCond %{REQUEST_FILENAME} !-d is working as I can go to "/js/tinymce" in this example and see directory listing.
Thanks.
I suggest you to change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^ajax/([^.]*)\.htm$ index.php?route=$1&opformat=ajax [L,QSA,NC]
RewriteRule ^([^.]*)\.htm$ index.php?route=$1 [L,QSA,NC]
Related
I am trying to figure out (learn) how to set up .htaccess so multiple Laravel installs could be run on the same IP by by using the URL (/site_1, /site_2) like:
http://xx.xx.xx.xx/site_1
So far what I have realized that subsequent request for resources (like: /css/app.css) don't pass the rewrite rules and are lost. I have tried the OR flag to tie the request somehow to the url but then rules gets mixed up.
How could I get this to work?
.htaccess
RewriteEngine ON
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} /site_1 [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]
RewriteCond %{REQUEST_URI} /aloe [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site_2/public_html/public/$1 [L]
Add this block and try again. Hope this will work. It might need some modification for the path you want to access for css and js.
RewriteBase /site_1/public_html/public/
RewriteCond %{REQUEST_FILENAME} \.(css|js)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /site_1/public_html/public/$1 [L]
I'm trying to create a vanity url for where users profile could easily be viewed by others.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/test/public/profile.php?username=$1 [NC]
This is the code but each time i use this code in my .htaccess file, i get 500 Internal Server Error and the sub-folder public disappears from my directory. What am i getting wrong? Do i need any to do any configuration first?
It's better to use global rewrite logic:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
and in Your index.php catch $_SERVER['REQUEST_URI'] and if user exists so respond if not then pass processing to next procedure.
But if You insist on realization as in question so try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?username=$1 [L,QSA]
Check this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^profile/([a-zA-Z0-9_\-/]+)$ test/public/profile.php?username=$1 [L,NC]
</IfModule>
However note:
You must have the rewrite engine on to make it work (google a bit for the installation procedure the OS/system you are working)
Put proper RewriteBase directive if you use it from subdirectory.
My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations
The real url format is
http://localhost/myfolder/index.php?url=login/register&step=1
After mod rewrite I get
http://localhost/myfolder/login/register/step/1
All good but how can I get the value of step in a php file.If I do $_GET['step'] then I get nothing.
My htaccess
Options -MultiViews
# turn rewriting on
RewriteEngine On
# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*) index.php?url=$0&step=$2 [L,QSA]
Change your rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]
I have a .htaccess file with the typical wordpress mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
However I need to allow a certain page to not get redirected. that being 'domain/ee'
I tried adding:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}%{REQUEST_URI} [L]
to the top. As I read it, that should have checked to see if the page existed and if so not run the reset of the rewrite. However it's not working.
any ideas? thanks.
Make the first rewrite rule something like this:
RewriteRule ^(path/to/page)$ $1 [L]
These two rules should prevent rewriting any actual files or directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d