Skip .htaccess rules for some particular files - php

Now i am using the following .htaccess rules to modify my urls.My website contains more images and similar files.I found that .htaccess rules increases site's loading time.I wish to skip execution of some codes for domain.com/images and domain.com/scripts folders.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+)/([0-9]+)/([^.]+).html index.php?perma=$3
RewriteRule ^movies/([^.]+).html gallery.php?movie=$1
RewriteRule ^album/([^.]+).html gallery.php?album=$1
RewriteRule ^img/([^.]+)/([^.]+).html gallery.php?img=$2
RewriteRule ^movies.html gallery.php
i want to modify it to
if it is a file in image or script folder skip following
else
RewriteRule ^([0-9]+)/([0-9]+)/([^.]+).html index.php?perma=$3
else
RewriteRule ^movies/([^.]+).html gallery.php?movie=$1
else
RewriteRule ^album/([^.]+).html gallery.php?album=$1
else
RewriteRule ^img/([^.]+)/([^.]+).html gallery.php?img=$2
else
RewriteRule ^movies.html gallery.php
Any way ?

Instead of having one .htaccess file put one in, for example, the movies folder with the RewriteRule for the movies folder or put one in the img folder with only the RewriteRule that is used in/for the /img/ folder. This means that it will load different .htaccess files based on its directory instead of using one.

Related

HTAccess Rewrite affecting other files in the Root?

I'm on a 1and1 shared server containing my other websites.
In the Root I have an HTAccess file Rewriting a photo viewing php file with an ID as a parameter.
RewriteEngine On
RewriteRule ^([^/d]+)\.html?$ /photography/albums/viewphoto.php?id=$1 [QSA,NC,L]
Example:
Current URL:
http://www.example.com/photography/albums/viewphoto.php?id=C01-A01-P01
Desired URL:
http://www.example.com/C01-A01-P01.html
All works for the rewrite of the URLs, but the html files located in the root have stopped including my PHP code functions and variables held in an .inc file.
Am I missing something in making the rewrite specific to a single file or is an option missing on the rewrite?
You need to use a RewriteCond to exclude existing files from the Rewrite ,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html?$ /photography/albums/viewphoto.php?id=$1 [QSA,NC,L]

htaccess rewrite condition to a php file in WP

I have this htaccess file in the 'public_html' folder:
RewriteEngine On
RewriteRule ^asset/(.*)$ asset.php?code=$1 [NC]
Essentially, the rewrite condition should work as follows. When a user clicks on a link ../asset/XXX, the asset.php file generates a new webpage containing details of the XXX asset.
The lookup is not working as I think the htaccess file cannot find the 'asset.php' file.
The asset.php file is located in
'public_html/wp-content/themes/theme1/asset.php'
How can I modify the htaccess file to lookup this file?
Have you tried:
RewriteRule ^asset/(.*)$ wp-content/themes/theme1/asset.php?code=$1 [NC]
The rewrite is from your directory where the htaccess file is located. So add the path to the destination file.
RewriteEngine On
RewriteRule ^asset/(.*)$ wp-content/themes/theme1/asset.php?code=$1 [NC]
Perhaps you can set the RewriteBase to /. Sometimes that fixes some problems.

.htaccess specified url with base file

I have a directory site and its sub-folder and files in it.
It also contain a .htacess and below is the code
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?cc=$1
so my below url will is effective and working good.
http://localhost/site/sweetinc
And below code in index.php below
<?php
if (isset($_GET['cc']) and !empty($_GET['cc'])) {
echo $_GET['cc'];
} else {
die("Sorry wrong code");
}
?>
And this is working good.
Now, i want to access or display all files as normally
http://localhost/site/sweetinc/home.php
http://localhost/site/sweetinc/test.php
home.php and test.php are located in site directory. And if it redirects to other sub-folders then it should
be visible like
http://localhost/site/sweetinc/sub-folder1/test3.php
Is this possible, as I am working seperating a group using seperate directory using .htaccess and considering all files in site directory as base files
The trick here (if I understand your question correctly) is that you don't want the rewrite rule to be in effect when you're trying to reach a file that actually exists, right?
In that case, just before your RewriteRule, add these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That way, the rewriterule only takes effect when the url (/site/sweetinc for example) does not really exist on the server. If it does (/site/sweetinc/home.php) the rewriterule is skipped and the file is shown.
(-f checks if the filename exists, -d checks if the directory exists, so /home/sweetinc/somedir/ should work too)
Update based on updated question
You need two separate rules for this. First of all, if the /sweetinc/ directory in the url is used, refer them to the /site/ folder:
RewriteRule ^sweetinc/(.*)$ /$1 [L]
Then, if the file does not actually exist, let the index.php handle it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cc=$1 [L]
Some examples:
I have the following files in my files subdomain:
/index.php (which has the same content as yours does, so it reads out ?cc=
/circle3.PNG
/the .htaccess with the above rules
http://files.litso.com/sweetinc/hello shows the index.php that echoes "hello"
http://files.litso.com/sweetinc/circle3.PNG shows the actual file in /
(note, I don't have a /sweetinc/ directory, it's all faked)

.htaccess in subdirectory

I've been searching the whole evening for a solution/approach for my problem with my .htaccess file.
Basically I have www.site.com with a .htaccess in the www directory with the following content:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/admin)
RewriteRule ^[^_+/]. index.php
It works just fine and allows www.site.com/en/articles/Interesting_title/3 to be parsed by index.php which reads which controller, language and what article to display.
However I'd like for the admin system to work the same way. www.site.com/admin should have an .htaccess files that allow me to write URL's this way.
www.site.com/admin/en/articles/article_title/3 should allow me to edit article number 3 using en english UI.
Dumping $_SERVER['REQUEST_URI'] in the first case gives "en/articles/Interesting_title/3"
Dumping $_SERVER['REQUEST_URI'] in the second case gives "admin/en/articles/article_title/3"
If I at a later point choose to move administration to www.site.com/shassooo I would like to avoid changing any code other then the .htaccess files.
Cheers
Append this line in the same .htaccess file you have, not under admin sub directory:
RewriteCond %{REQUEST_URI} !^/admin/index.php$ [NC]
RewriteRule ^admin/[^_+/]. /admin/index.php [L,NC]

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories