I've been trying to exclude png and jpg files from being excluded from the rewrite condition. Currently, I have a set of pictures that are being presented as broken images. What is causing the error is a rewrite rule in my .htaccess. That rule overwrites the URL of a folder so it will appear with a different name in the URL, instead of /volley it appears as JeepVolleyballChampionship. I've tried to get rid off the last part that redirects to the home page if a directory doesn't exist, but nothing seems to work. I've also tried to include an exclusion of files with extensions of png and jpg from the rewrite block that rewrites the directory...
What I assume is that I'm not implementing that exclusion correctly. My page still loads fine, except with the broken images. Can you provide insight of what I'm doing wrong in this block?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
My whole .htaccess is provided here:
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
DirectorySlash Off
RewriteEngine on
#try to make it so that /volley/ is eliminated from folders and name changes but it is still under this structure
RewriteRule ^volley$ /volley/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/$ /volley [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/index.php$ /volley [R=301,L]
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
#non-existent folder to default root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
jpg and jpeg are treated as separate extensions here. Add jpeg as well on the line no 3. It may fix your issue, if your extension is "jpeg".
It will look as follows:
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]
You should not be entering "RewriteEngine on" multiple times, and you also need to be careful about the order of your rules. Off the top of my head I think all you should need is this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/volley /JeepVolleyballChampionship [R=301]
The !-f condition will prevent the images from being rewritten (along with all other files) so long as they exist, this is the canonical way to exclude all existing files like js, css, etc.
Then the pattern in the rule will only rewrite URLs that start with /volley, so you do not need a condition to match it -- in fact, the rule pattern is checked BEFORE the conditions, so matching there is faster than matching everything and then checking the conditions.
Related
I'm trying to shorten my URL but sadly can't find anything that helps.
I divide my code in folders. Index is positioned at root just like my .htaccess.
The folders are named like the file extensions, so php, js, css [...]
I have a link like the following:
localhost/php/getBets.php
and want it to be
localhost/getBets/
I already have the part that cut's the .php extension at the end, so here is my full .htacces
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Hide Index
IndexIgnore *
# Forbid accessing certain sites
RedirectMatch 403 ^/.gitignore$
RedirectMatch 403 ^/.htaccess$
RewriteRule ^(?!index)(?!.*getBets).*\.(php|rb|py|txt|md|sql|inc)$ - [F,L,NC]
# Hide .php file ending in URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Can someone maybe tell me how I could achieve this? :)
Thanks alot!
For your required url you can use below rule in root directory it is for rewriting,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ php/$1.php [L]
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
Use %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]
Fixed it. Did a small change on the file. It can be seen below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [NC,QSA,L]
I have been trying for a whole day to fix an issue I'm having. I have this .htaccess file which simply rewrites the urls just like
http://example.com/about/?req=true
http://example.com/index.php/about/?req=true
It works on my localhost without a problem, but when I upload it to the server I get this "no input file specified" error. You can see the content of the .htaccess file below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php/$1 [L]
I tried different things I found on the internet including adding a php.ini file which sets cgi.fix_pathinfo to 1, but none of them worked except one and that one doesn't read the request string. You can see that one below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php?x=$1 [L]
Thanks in advance.
First off all check whether rewrite module is enable in apache or not
if enable then u can use this code hide index.php with in url
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /base_directory_name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /base_directory_name/index.php [L]
</IfModule>
I think this will help u .
I have the following .htaccess:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]
But I would also like to remove .php extension with the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I have tried a dozen combinations based on answers elsewhere not just on StackOverflow but still cannot get it right, I either render pages not in shared directory unable to open with 500/404 errors or 500 error when I go to /shared.
After further investigation and trial when I add the rules to remove the .php extension it messes up the first rule to route anything under the path of /shared/ to shared.php the path /shared/username are not real locations but the script insures that the correct information is presented. It would be handy to ignore the second rule if the URL has /shared/ in the path? Is that possible? I am not rewriting everything to the /shared/ directory - only when the path reads /shared/username do I want that rule to kick in, everything else should be rewritten to the / base directory.
Keep your rules like this:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]
I have the following .htaccess file in a subdirectory of a site which allows me to route all URLs to index.php where I can parse them.
However, it's not allowing the standard files that I need for the website, e.g. css, javascript, pngs, etc.
What do I need to change (I assume in the fourth line) to allow these files so they don't get routed to index.php?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|png|jpg|gif|robots\.txt)
RewriteRule ^(.*)$ index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php
Something I noticed. You're using the forward slash instead of a question mark... the params redirect would normally look like this:
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
This should work by itself since any of those files *should* be real files.
ErrorDocument 404 /index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
To have the site ignore specific extensions you can add a condition to ignore case and only check the end of the filenames in the request:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
If you're trying to ignore a folder then you could add:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(public|css)
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
The easiest is to ignore them explicitly early in your rules:
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteRule ^(index\.php|robots\.txt)$ - [L]
This avoid carrying them around all over the place with RewriteCond.
At your option, check that the file exists prior to doing so:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(index\.php|robots\.txt)$ - [L]
(Note that the file check generates an extra disk access.)
You have the right idea, tweaking the fourth line.
The ^ is saying the various matching strings must be at the beginning of the line. If you don't care where any of these appear in the file, you can just remove the ^. That will avoid rewriting *.css, *.js, etc.; but will also not rewrite publicideas.html.
If you want to limit to just the suffixes, try this:
RewriteCond $1 !^(index\.php|public|.*\.css|.*\.js|.*\.png|.*\.jpg|.*\.gif|robots\.txt)$
This says to match anything at the beginning, then a ., then the suffix. The $ says match these at the end (nothing following).
I'm not sure about the public, so I left it (which means exactly public, with nothing else - probably not what you meant, but you can add a * before or after, or both).
RewriteCond %{REQUEST_FILENAME} !-f alone should be enough.
Another option is t exclude specific files from the rewrite.
From the TYPO3 packages:
# Stop rewrite processing, if we are in the typo3/ directory.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
This rule should appear before your actual rewrite. It should be
RewriteRule ^(public/|*\.css|*\.js|*\.png|*\.jpg|*\.gif|robots\.txt) - [L]
in your case.
I'im trying to make a admin system for a site, but the htaccess ruins it D:
The admin part is in a folder named admin.
My htacces to far:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin)$ admin/index.php
RewriteRule ^([\wæøå]+)$ index.php?page=$1 [QSA]
RewriteRule ^([\wæøå]+)/$ index.php?page=$1 [QSA]
RewriteRule ^(nyhed)/([\w\d\-æøå]+)$ index.php?page=$1&nyhed=$2 [QSA]
RewriteRule ^(nyhed)/([\w\d\-æøå]+)/$ index.php?page=$1&nyhed=$2 [QSA]
But won't let me in that folder?
So I did a if in my index:
if($_GET["page"] == "admin"){
header("location:http://google.com");
}
But that won't do anything either, and no errors D:
RewriteCond lines can be strung together as you have them, but they only impact the first rule they come to.
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
This will send all requests for non-existent directories and files to index.php. Files and directories that exist won't be impacted. The other rules are unnecessary.
This is your problem:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You are rewriting to a directory named admin which obvously exists in your file system as such Apache says:
if this path does not exist as a directory path or as a file path continue with the rewrites
That is why it is not quite rewriting the way you would expect.