apache mod_rewrite affecting files inside sub folder - apache

Options +FollowSymlinks
RewriteEngine On
RewriteBase /mysite
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?p=$1&name=$2 [NC,L]
RewriteRule ^([^/\.]+)/?$ page.php?name=$1 [NC,L]
The above code is written in my .htaccess file. I want my URL
from:
http://localhost/mysite/page.php?p=categoryname&name=article_title
http://localhost/mysite/page.php?&name=articel_title
to:
http://localhost/mysite/category/article_title
http://localhost/mysite/article_title
Those rules are working fine but if I try to access my back-end for the admin which URL is http://localhost/mysite/admin` it redirects the page to page.php?name=admin.
Is there a way to just limit the rule only for the page.php and not affecting the other pages under sub-folder which is admin?

You could use a rule to stop /admin from beein rewritten:
RewriteRule ^admin$ - [L]
Put that rule before the others and it will be the only rule that is applied to /admin.

If you add this rewrite condition before your rules, it will only apply the rules if the request is not for a real file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ page.php?p=$1&name=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ page.php?name=$1 [NC,L]

Related

htaccess URL rewrite file/param/value format

I am writing a htaaccess rule to rewrite my below URL
http://example.com/edit_user.php?enc=aWQ9MQ%3D%3D
as
http://example.com/edit_user/enc/aWQ9MQ%3D%3D
I tried with http://www.generateit.net/mod-rewrite/index.php but it gives me something like this
RewriteEngine On
RewriteRule ^enc/([^/]*)$ /edit_user.php?enc=$1 [L]
But this is fixed rule for edit_user.php file, I want a rule that would work for all files in same fashion.
Also if user types in http://example.com/edit_user/ the page should redirect to http://example.com/edit_user.php since there are no parameters.
Have this code in root .htaccess:
RewriteEngine On
RewriteRule ^([\w-]+)/enc/([^/]+)/?$ $1.php?enc=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [L]

Rewriting folder won't work

I have an app stored on my server in "SERVER_ROOT/app/1.0.1/index.php". The app is accessed by typing in "example.com/index.php" and I use a rewrite to achieve this.
The issue I am having is I want users to access "SERVER_ROOT/app/1.0.1/image/" by typing "example.com/image/" but none of my rewrites work when I edit the .htaccess file.
Here is my .htaccess file:
Options -MultiViews
RewriteEngine on
RewriteBase /app/1.0.1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) index.php/$1 [L]
RewriteRule ^image/$ app/1.0.1/image/ [NC,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks,
Peter
Since the /image/ directory doesn't actually exist, it'll get trapped by the first rule that routes everything to index.php. Assuming your htaccess file is in your document root, you need to add the rule above your routing rule and change it to this:
RewriteRule ^image/(.*)$ image/$1 [L]
If your htaccess file isn't in the document root, then you need to add this rule to the top of the htaccess file in your document root:
RewriteRule ^image/(.*)$ /app/1.0.1/image/$1 [L]

htacess rewrite rules conflict

I am having issues trying to set up a correct htaccess file.
What I basically want to do is to implement clean URL and hide the .php extension except for one file.
What I currently have set up is the following:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
#RewriteRule ^(?!recruit)(.*)$ $1.php [NC,L]
The first rule will take anything after 'recruit' and pass it as a get variable in the url
www.example.com/recruitHI --> www.example.com/recruit.php?id=HI
What the other rule needs to do is to append .php to anything else other than anything that starts with recruit.
www.example.com/index --> will look for index.php
www.example.com/contact --> will look for contact.php
www.example.com/recruit --> Needs to be ignored because of the first rule
When I have the 2 rules on and start apache, I get an error saying my configuration is wrong. They both work individually though.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
2nd rule will add .php extension only for URIs that are not file or directories and that are already valid php files.
Try adding a condition to the second rule so that it won't blindly append a php to the end of the URI:
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Specifying RewriteBase at the beginning. If everything is in the site root it would be
RewriteBase /
Otherwise all your Rules should begin with ^/
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ $1.php [NC,L]
Since your have the L The rules after recruit will only affect items that don't have it. But instead of having a script for every url possibility, you should look at using a single Front Controller.
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
RewriteRule ^(.*)$ index.php [NC,L]
Then you can use FallBack provider instead (newer in Apache)
RewriteEngine On
RewriteBase /
RewriteRule ^recruit(.*)$ recruit.php?id=/$1 [QSA,L]
FallbackResource /index.php

htaccess issue causing internal server error too many redirects

so I am playing with .htaccess to have clean URLs in my codeigniter app.
in short, i am trying to:
1) remove index.php in urls (redirect permanent)
http://localhost/directory/index.php*
to
http://localhost/directory/*
http://my.domain.com/index.php*
to
http://my.domain.com/*
2) rewrite requests for certain controllers to index.php/[controller_name]
http://localhost/directory/controller1*
to
http://localhost/directory/index.php/controller1*
http://my.domain.com/controller2*
to
http://my.domain.com/index.php/controller2*
my htaccess file currently goes like this:
Options +FollowSymlinks
RewriteEngine On
#RewriteBase /
# Redirect index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^(.*)index\.php((/)(.*))?$ /$1/$4 [R=301,L]
first issue:
this does not work for http://localhost/dir/index.php/controller1.
instead redirecting to http://localhost/dir/controller1, it redirects to http://localhost//controller1 ($1 return empty string?)
# Rewrite CI certain controllers
RewriteCond %{THE_REQUEST} directory/(home|other_controller) [NC]
RewriteRule ^(.*)(home|other_controller)(.*)$ /$1index.php/$2$3 [NC,L]
second issue:
this does not work for http://localhost/dir/home gives internal server error (too many redirects).
but if I test added R=301 code, it successfully redirect to http://localhost/dir/index.php/home. but this is not my intention to redirect, I only need to rewrite it.
please advise.. :)
Try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You can place this in the directory the bootstrap file (index.php) is in.
If you have FastCGI implementation, you need to add a question mark in the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Apache ignore htaccess

I have a website that uses mod_rewrite for pretty urls. I have two main urls:
example.com/id
example.com/generate/id
And this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^generate/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]
RewriteRule ^([a-z0-9_-]+)$ contact.php?id=$1 [L,NC]
The first url is working correctly, but the second not. Apache shows a 404 error, I think that it's because it's looking for the folder "generate", that doesn't exist, and it can't find the htaccess in the document root.
Thank you
Have you try
RewriteRule ^generate\/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]