.htaccess RewriteRule if exists in subdirectory - .htaccess

I had some experience with .htaccess and the mod_rewrite but I can't get this to work. Basically I wan't all URLs redirected to index.php EXCEPT those who point an existing files in the "public" subdirectory.
For example, if I have a file like /public/logo.png I want this:
http://example.com/logo.png -> /public/logo.png
http://example.com/logo2.png -> index.php (logo2.png does not exist)
http://example.com/whatever -> index.php
Thank you :)

Thanks to Amine help I finally solved this, even though this looks quite messy:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ - [L]
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ /public/$1 [L]
RewriteRule ^(.*)$ /index.php [L]
Hope it helps

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ /public/$1 [L]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(.*)$ /index.php [R=301,L]

Related

Hide a subfolder in the url with htaccess

I am using this url:
http://example.com/folder/form.php
I want whenever someone uses this link to change the URL shown to
http://example.com/form.php
So far I have done that:
RewriteEngine On
RewriteRule ^$ folder/
RewriteRule ^(.*)$ folder/$1
BUT, I dont want this rule to apply when someone uses
http://example.com/index.php
So I need to make this rule work only when the url is like
http://example.com/form.php
Thanks in advance!
If you do not want your /index.php file to redirect to /folder you can exclude it in the pattern or using a RewriteCond
RewriteEngine On
RewriteRule ^$ folder/ [L]
RewriteRule ^((?!index.php).*)$ folder/$1 [L]
or
RewriteEngine On
RewriteRule ^$ folder/ [L]
RewriteCond %{REQUEST_URI} !^/index.php [NC]
RewriteRule ^(.*)$ folder/$1

How can i show beauty url with htaccess?

I'm using MVC with PHP and, for example, I have this URL:
domain.com/{modue}/{action}
This will make it look like:
domain.com/user/join
I want it to show
domain.com/join
instead.
Can anyone help me?
I tried this with an .htaccess file, but I couldn't. This is my code:
RewriteEngine On
RewriteRule ^$ ./controller.php [L]
RewriteRule ^([a-zA-Z_0-9-]+)/$ /$1? [R=301,L]
RewriteRule ^([a-zA-Z_0-9-]+)$ /controller.php?modulo=$1&%{QUERY_STRING} [L]
RewriteRule ^([a-zA-Z_0-9-]+)/([a-zA-Z_0-9-]+)/$ /$1/$2? [R=301,L]
RewriteRule ^([a-zA-Z_0-9-]+)/([a-zA-Z_0-9-]+)$ ./controller.php?modulo=$1&action=$2&%{QUERY_STRING} [L]
Try this if you have just one controller -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1
RewriteRule ^(.*)$ user/$1 [L]
</IfModule>

Rewrite with htaccess

Here is the what I'm trying to do. I've got these urls, Old form and New form
site.com/video.php -> site.com/video
site.com/index.php?cat_id=1 -> site.com/some-cat-name
Here is my htaccess file which is working only if one of the last two is commented.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond $1 !^(index\.php|img|js|css|admin|robots\.txt)
RewriteRule ^video$ /videos.php/$1 [L]
#RewriteRule ^(.*)$ /index.php/$1 [L]
if those two are active the server returns error 500
RewriteRule ^video$ /videos.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
my logic is if first is matched video in the url redirect to videos.php and forget for all under this line, but seems to be in other way. Can some one to explain how to have some predifined urls and all others to go on index.php, tnx in advanced.
You are getting 500 (Internal server error) because your rule is looping in this rule:
RewriteRule ^(.*)$ /index.php/$1 [L]
To fix this rule have your .htaccess like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|img|js|css|admin|robots\.txt)
RewriteRule ^video$ /videos.php/$1 [L]
# ignore existing files and directories from rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond and RewriteRule exclude specific folder

I am using the following .htacces file in my domain root.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|admin|cache)
RewriteRule ^(.*)$ index.php [L]
One problem, is when I try to open the file located at cache/themes/theme1/global.css
But for some reasons it doesn't exclude the folder cache from the RewriteRule
Anyone know a solution for this?
I didn't test this but should work;
RewriteEngine on
RewriteRule ^(cache.*)$ $1 [L]
RewriteCond $1 !^(index\.php|images|robots\.txt|admin|cache)
RewriteRule ^(.*)$ index.php [L]

.htaccess mod_rewrite question

If I have a url http://example.com/page.php
but I want the page to display when someone goes to http://example.com/page/
how do I go about doing that?
Try this:
RewriteEngine on
RewriteRule ^([^/]+)/$ $1.php
I usually use something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^$ index.php [L,QSA]
RewriteRule ^([^/\.]+)/?$ $1.php [L,QSA]
</IfModule>
Note the question mark after the slash. You could add that to Gumbo's example to "test" for a trailing slash (so it can be there or not).

Resources