.htaccess started redirecting images - php

So I've been messing around my website (which was working yesterday) and by mistake I've rewritten my .htaccess file with blank one (wanted to duplicate the file in one folder into different folder, but messed it up making it rewrite root folder's .htaccess), so I'm trying to make one again (yes I know, should have had backup).
Now to the problem: I was editing unrelated thing in my .htaccess file, and suddenly the images stopped showing, being redirected to my 404 error page instead. It looks to me like the Apache suddenly started ignoring my certain RewriteCond rules, as css and javascript rules normally work, and it's driving me insane.
I've tried to search everywhere, but haven't found the solution.
This is my current .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !=/index_new.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index_new.php?sekce=$1 [NC,L]
RewriteRule ^([^/]+)/$ index_new.php?sekce=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)$ index_new.php?sekce=$1&podsekce=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3&podpodpodsekce=$4 [NC,L]
The question was marked as DUPLICATE, but I've tried the solutions (before writing the question) and they do not work, as I use absolute paths to images too, but I cannot get to them regardless.

Managed to get the backup from the webserver provider. If anyone would have same kind of problem, the working code is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule \.(css|jpe?g|gif|png|gif|js|ico)$ - [L]
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^([^/\.]+)/?$ /index_new.php?sekce=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index_new.php?sekce=$1&podsekce=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index_new.php?sekce=$1&podsekce=$2&podpodsekce=$3&podpodpodsekce=$4 [L]

Related

apache - Changes to RewriteRule in .htaccess not taking effect

I am using WAMP and I want to remove index.php from my url and I used this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Then I had to change two lines as I moved my files to sub-directory OOP-Project from:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
and
RewriteRule ^(.*)$ /index.php/$1 [L]
to:
RewriteRule (.*?)index\.php/*(.*) OOP-Project/$1$2 [R=301,NE,L]
and
RewriteRule ^(.*)$ OOP-Project/index.php/$1 [L]
But, the changes are not taking effect although first time it worked! I tried adding syntax error and it showed the error. Surprisingly, when I moved my files to another folder say OOP-Project-1, it worked. I did a lot of research and found this Changes to RewriteRule in .htaccess not taking effect But it didn't address the problem correctly.
Hope someone helps!

url rewriting not working on one.com provider

Currently i am trying to get url rewriting to work on one.com hosting provider which is a pain and support don't understand the issue, so i was thinking of asking here if you may assist me finding the issue.
I'm using this rewriterule which worked on localhost but not with the provider:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?key=$1&seo=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?key=$1&seo=$2
</IfModule>
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ http://www\.mysite.com/$1 [R=permanent,L]
and in index:
if($key=='post') //Cant include $seo here because the variable differ for
//each post on the site
{
include('post.php'); // Post page
}
Then in post.php ofc i take the seo GET variable to display the post content on the page.
complete url www.mysite.com/post/test-test-test
The error i end up with is 404
Not Found
The requested URL /post/test-test-test.php was not found on this server.
So basically what i understand is that it tries to enter a folder named /post/ and look for the file test-test-test.php
So i believe it doesn't include the rewrite rule, or can you find an error in the rewrite rule which worked on localhost?
You need to rearrange your rules and for adding .php extension make sure a file with .php exists:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
# ignore rest of the rules for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?key=$1&seo=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

CodeIgniter and Old-style PHP

I have a domain, let's say somedomain.com. I have an admin page somedomain.com/admin. The pages were coded in plain PHP. I recently re-wrote somedomain.com in CodeIgniter. But I do not want to touch somedomain.com/admin for now.
How do I make this work? When I go to somedomain.com/admin, it is looking an admin controller, which of course does not exist. I have an index.php file in admin folder, I cannot figure out a way to access the index file.
Thank you.
You should post .htaccess for us to se. But anyway. You should exclude "admin" to be directed to index.php
something like this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(ACT=.*)$ [NC]
RewriteRule ^(.*)$ index.php?%1 [L]
RewriteCond $1 !^(admin|index.php) [NC]
RewriteRule ^(.*) /index.php?/$1 [L]
Like this :
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index\.php|assets|thirdparty|admin|themes|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

How to remove .php from all urls with no trailing slash?

I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:
website.com/about.php -> website.com/about
I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:
RewriteRule about about.php [L]
I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.
Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2).php$ https://website.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L,QSA]
You can use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
The below answer can be found here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

htaccess remove file ext for folder

I've tried looking at other tutorials and examples but nothing quite fits my scenario. My htaccess file looks like this:
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^view/([^/]+)/([^/]+)/?$ index.php?page=view&id=$1&title=$2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+)
RewriteRule ^([a-zA-Z0-9]+)$ /%1/? [R=301,L]
ErrorDocument 404 /404
I'm using PHP and need to rewrite /index.php?page=page&moreparamaters=1
But I also have a folder on the server called /admin/ and I want to remove the .php extension from the files in this folder so that:
/admin/lol.php becomes /admin/lol/
I have lots of these files so I can't really add each one in, individually. How can I do this without affecting my index.php rewrites?
Thanks
you have to think the other way round. Assuming that all your files are placed in the folder directly (meaning, no subfolders), this will work:
RewriteRule ^admin/([^\/]*)? /admin/$1.php [L,R]
In your links you may then write /admin/lol/ or /admin/lol, both will be redirected to /admin/lol.php

Categories