Exclude folders when redirect with htaccess - .htaccess

I have a question.
I want to redirect domain.com/var to domain.com/id=var
I can do that, but when users write a var like my folders, the htaccess redirect to the folder first :/
This is my actual htaccess
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9-+/]+)$ index.php?id=$1 [L]
RewriteRule ^([a-zA-Z0-9-+/]+)/$ index.php?id=$1 [L]
But when the user write for example css, the htacces redirecto to domain.com/css/?id=css
And the second problem is if the user put / after the var, example domain.com/var/
How can I change that? Thx 4 all!!

You need to skip files/directories from rewriting:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9+/-]+)/?$ index.php?id=$1 [L,QSA]

Related

Getting value with symbol from htaccess

This url
example.com/ad/load.php?id=pay.jpg
would become
example.com/ad/pay.jpg
I tried to do this.. but it doesn't work.
Here is my htaccess (into ad folder)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /ad/
RewriteRule ^(\w+) ./load.php?id=$1
Edit
on this root example.com/ad/ I want to show index.php but it does not show. how to fix it
You regex should allow dot also. Try this in /ad/.htaccess:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /ad/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w.-]+)/?$ load.php?id=$1 [L,QSA]

How do I do simple flat link to .php file redirect?

My current .htaccess file is as
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php [L]
This is at the directory level, I do not want to have a site-wide .htaccess.
I can redirect ../file.html to ../file.php but I am not able to do it for ../file -> ../file.php
My URL would be www.domain.com/sub1/sub2/file/ The file that should be run is www.domain.com/sub1/sub2/file.php But I want this rule to work only under sub2. So the rule will be included in the .htaccess at sub2
What exactly I need to change on this code to be able to do that?
More specifically, why is this not working?
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)$ $1.php [L]
This is working as I want it to work:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L]
You can change your existing rule to this to work under /sub1/sub2/.htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /sub1/sub2/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ $1.php [L]

Trouble with .htaccess mod_rewrite

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php
Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>
I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

htaccess with specific folder exception

I've set up an htaccess file in order to create a small routing structure where urls redirect to index.php. I want to add an exception where if the user types in "/admin" and anything after, the url instead redirects to admin.php.
htaccess -
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Example: http://mysite.com/admin/settings/ should lead to admin.php?page=settings, while http://mysite.com/about/ should lead to index.php (not using any parameters here). Regexp is really not my cup of tea, especially when there are multiple conditions to consider. Ideas?
Edit: ^admin/([^/]*)/? solved it.
You may try this in one .htaccess file in root directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !admin\.php [NC]
RewriteRule ^admin/([^/]*)/? /admin.php?page=$1 [NC,L]
RewriteCond %{REQUEST_URI} !/admin/ [NC]
RewriteRule .* /index.php [NC,L]
For permanent redirection, replace [NC],L] with [R=301,NC,L]

htaccess redirect subfolder to index with variable

I'm trying to redirect users with htaccess. I want to redirect any wildcard subfolder to the index with subfolder as a php variable.
Example:
http://domain.com/abc
Change to:
http://domain.com/index.php?x=abc
I'm pretty sure I can do this is htaccess although I'm not 100%. Thanks!
This should be easy, use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?x=$1 [L,QSA]
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^abc /kat.php?index.php?x=abc [L]
############ or dynamic
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.+) /kat.php?index.php?x=$1 [L]

Resources