How to exclude base url from some rewrite rule in htaccess? - php

Hey everyone I have a rewrite rule in .htaccess file which is
RewriteRule ^([a-zA-Z0-9\-\_]+)?$ product.php?product=$1 [NC,L]
When I go to www.domain.com it apply the above rule and redirect to product.php and when www.domain.com/index.php it works fine. Where I am doing wrong. Thanks

Remove ? in your regex which is making whole pattern optional and exclude directories from this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ product.php?product=$1 [QSA,L]

<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /your_project_folder_name/index.php/$1 [L]
</IfModule>

Related

How to write multiple .htaccess rewrite rules?

I have multiple php pages like this:
http://www.example.com/si/article.php?id=122&nTitle=my-title
http://www.example.com/si/post.php?id=352&pTitle=my-post-title
i would like to rewrite them to
http://www.example.com/si/122/my-title
http://www.example.com/si/352/my-post-title
I tried different things in my .htaccess file and no result. Please tell me how to make to work with multiple rules. If I use one rule in .htaccess file its work for one php page like below:
RewriteRule ^([\s\w-]+)/(.*)/?$ article.php?id=$1&nTitle=$2 [L,QSA,NC]
This is now I tried it for multiple pages:
<ifModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /article.php?id=$1&pName=$2 [L,QSA,NC]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^/([\s\w-]+)/(.*)$ /post.php?id=$1&pName=$2 [L,QSA,NC]
</IfModule>
Can anyone give me an advice on how to use this functionalities in .htaccess?
Thank you.
You can use url like : (look like different from each other)
http://www.example.com/arical-si/122/my-title
http://www.example.com/post-si/352/my-post-title
then redirect to :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$0
RewriteRule ^artical-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ article.php?id=$1&nTitle=$2 [L]
RewriteRule ^post-si/([0-9+/=$]*)/([A-Za-z0-9+/=$]*)$ post.php?id=$1&nTitle=$2 [L]
</IfModule>
First of all you should test your regex, you can do it there regex generator
Then in your htaccess, you can put your regex like this:
RewriteEngine on
RewriteRule ^/post.php?id=([0-9])&pName=([a-z]+)$ post/$1/$2 [L]
RewriteRule ^/article.php?id=([0-9])&pName=([a-z]+)$ article/$1/$2 [L]
try it step by step and you can debug it easily.

htaccess - Need help writing the correct rule

I'm using SimpleMVCFramework
All my routes are working fine, based on the default htaccess file:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
I have a route that expects an id:
http://localhost/mpl/servicos/smvcf/detalhe/37343
But I need the URL the user sees to be friendly as:
http://localhost/mpl/servicos/smvcf/mercedes_benz-a-a_220_cdi_auto-37343.html
I thought something like this would work, but I get a 404:
RewriteRule ([^/]+)-([^/]+)-([^/]+)-([^/]+).html detalhe/$4
Please help.
The user sees the pretty url, but you are not interested in what the seo-title is. What you are interested in is the number that is hidden in it. As the number is in the very back of the pretty url, let's just match on that:
RewriteRule ^[^/]+-([0-9]+)\.html detalhe/$1 [L]
If you are trying to use another rewrite that will not be routed through the default MVC route index.php then you need to make sure you place your new rewrite before those rules. Also that folder you are redirecting to will need to have a index PHP file to do something as well.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)-([0-9]+)\.html detalhe/$2 [L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

.htaccess file with some exceptions

I need a .htaccess file that follows these rules:
all files (and subfolders/subfiles) of the folders images, css, js, admin, fonts should be reachable
urls like bla.com/info should be redirected to bla.com/index.php?p=info
urls like bla.com/products/22 should be redirected to bla.com/index.php?p=products&cat=22
the url bla.com/products should be redirected to bla.com/index.php?p=products
Can anyone help me with this? This is what I got so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /staging/
RewriteRule ^favicon.ico favicon.ico [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?p=$1 [NC,L]
But it only works for the first 2 conditions...
Thanks!!
These 2 rules should work for you:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([a-z0-9-]+)/?$ index.php?p=$1 [NC,QSA,L]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?p=$1&cat=$2 [NC,QSA,L]
Try:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /staging/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)(?:/(.*?)/?|)$ index.php?p=$1&cat=$2 [L,QSA]

HTACCESS directories redirect

I'm trying to redirect the address
www.example.com/tops/articles/first_article
to the address
www.example.com/tops/test1.php?name=first_article
also to redirect the address
www.example.com/tops/galleries/first_gallery
to the address
www.example.com/tops/test2.php?name=first_gallery
and all other addresses like
www.example.com/tops/first_page
to
www.example.com/tops/page.php?name=first_page
The following htaccess file is giving me a redirect loop.
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^/tops/articles/(.+)$ /tops/test1.php?name=$1 [L]
RewriteRule ^/tops/galleries/(.+)$ /tops/test2.php?name=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1
Doe's anyone has any idea why is that?
What am I doing wrong?
Thanks in advance.
Does page.php exist in the root directory (or any other non /tops/ directory for that matter)?
If not, then that is probably your problem. You should change your last rule to something like:
RewriteRule ^tops/(.*)$ /tops/page.php?name=$1
Well, I finally got it working.
Thought I share it so if anyone every search for this question he will find it.
Here is the code that workd:
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^articles/(.+)$ ./test1.php?name=$1 [L]
RewriteRule ^galleries/(.+)$ ./test2.php?name=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*)$ ./page.php?name=$1 [L]
Enjoy !
You may try this:
RewriteEngine On
RewriteRule ^tops/articles/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/tops/test1.php?name=$1 [L]
RewriteRule ^tops/galleries/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/tops/test2.php?name=$1 [L]
RewriteRule ^tops/([a-zA-Z0-9-=_.]+)/?$ http://www.example.com/page.php?name=$1 [L]
Replace all 3 rules with these rules.
UPDATED for all characters:
RewriteEngine On
RewriteRule ^tops/articles/([^/]*)/?$ http://www.example.com/tops/test1.php?name=$1 [L]
RewriteRule ^tops/galleries/([^/]*)/?$ http://www.example.com/tops/test2.php?name=$1 [L]
RewriteRule ^tops/([^/]*)/?$ http://www.example.com/tops/page.php?name=$1 [L]
.htaccess should be in root directory If it is in /tops directory, try removing it from the pattern and the substitution URL.

htaccess Ignoring directory and its subdirectories

I have the following in my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(mailinglist)/.*$ - [L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I basically want to remove htaccess from hitting that last line if I am in the mailinglist directory.
This only works for items in the root of the /mailinglist directory. Once I go deeper like /mailinglist/w/1 it breaks and hits that last rewrite rule. How do I stop it from processing that last rewrite rule if I am in the /mailinglist directory.
The reason is I have a different set of htaccess in that directory and I do not want this htaccess to control it.
Try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(mailinglist)/.*$
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I just switched the checking for mailinglist to be a RewriteCond. The condition will only rewrite to index.php if the URI doesn't begin with mailinglist.
The conditions are being applied to the wrong rule, You need to swap around your rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(mailinglist)/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Conditions only apply to the immediately following rule, so the 2 !-f and !-d conditions are being misapplied to the passthrough, while the index.php rule is missing those conditions.

Categories