htaccess to rewrite file in subfolder (2nd level) to folder (1st level) - apache

Currently, I'm using the below htaccess code to load http://www.domain.com/folder/file.php from http://www.domain.com/file, removing the folder name "/folder/", and the file extension ".php", from the URL.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now, I'm trying to do almost the same, but with a file that is stored in a subfolder.
So, in other words, I want to call http://www.domain.com/folder/subfolder/file.php from http://www.domain.com/subfolder/file
In this case, hiding the extension file is not a problem as the 2 last lines from the code above works globally, the issue is with the Rewriting which doesn't work simply by adding the 'subfolder' name. I thought this was going to work, but it wasn't:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/subfolder/$1.php [L]
I appreciate any help.

Replace both of your rules with this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/folder/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

Related

How can I get in one spesific folder?

I have made clean urls in .htaccess file, but after that I dont get Access to a spesific folder. How can I get Access to /folder/?
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?side=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?side=$1 [QSA]
RewriteRule ^([a-z]+)\/([a-zA-Z0-9]+)\/?$ index.php?side=$1&id=$2 [NC]
RewriteRule ^rss/feed/([a-zA-Z0-9]+)\/?$ index.php?side=feed&id=$1 [NC]
You can use the following :
Options +FollowSymLinks
RewriteEngine On
##-if an existent dir or file is requested, serve it immideatly##
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
########
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?side=$1 [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/?$ index.php?side=$1&id=$2 [NC,L]
RewriteRule ^rss/feed/([a-zA-Z0-9]+)\/?$ index.php?side=feed&id=$1 [NC,L]

How to get htaccess to load page without file extension

I have searched around and tried a few different things, with no luck.
I am trying to load this page http://SERVERNAME.com/get.php when I use the url http://SERVERNAME.com/get
Here is what I have in my htaccess now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
I have tested to make sure my htaccess file is being read by intentionally breaking it and getting a 500 error.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]

multiple RewriteRules in htaccess file, the first one over writes the second one?

I am trying to run multiple RewriteRules in my htaccess file.
however, the first RewriteRule, overwrites the second RewriteRule for some reason!
this is what i have in my htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ items.php?itemsurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ items.php?itemsurl=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ blog.php?blogurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ blog.php?blogurl=$1 [L]
the first RewriteRule works fine but if I click on the links for blog.php?blogurl= it will simply take me to the items.php!
I could put the RewriteRules for the blog.php at the top of the htaccess file and put the RewriteRules for items.php bellow it and it will make the blog.php rewriterule work for blog.php but it will make the rewriterules for the items.php stop working and everything will point to the blog.php page!
So basically, only the first RewriteRule in the htaccess file works and it will overwrite the second one somehow.
could someone advise on this please?
Replace your code with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ items.php?itemsurl=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ blog.php?blogurl=$1 [L]
RewriteCond affects only to the first RewriteRule.
The apache variables should be in these brakes: {}, not these ()
And one more thing - to check . you should escape it so: \.
As discussed in comments, you can use following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^items/([\w-]+)\.html\/?$ items.php?itemsurl=$1 [L,QSA,NC]
RewriteRule ^blog/([\w-]+)\.html\/?$ blog.php?itemsurl=$1 [L,QSA,NC]

If/else and file_exists function in htaccess mod_rewrite

I don't really know how to explain this situation but i give it a try. So, I have this url:
http://localhost/static/some_picture.png, and my folders look something like this:
statics/x/picture.png
statics/y/logo.png
What I want is that if the user types the mentioned url, he will see the "picture.png" what is in the "statics/x/picture.png", but if he types http://localhost/static/some_logo.png then he will see the "logo.png" in the "statics/y/logo.png".
My last try:
RewriteEngine On
RewriteCond statics/x/$1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteCond statics/y/$1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
Currently i have this .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
You're on the right track, but your -f condition isn't right.
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^ /statics/x/%1 [L]
RewriteCond %{REQUEST_URI} ^/static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^ /statics/y/%1 [L]
You could try the following;
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
the trick is to use RewriteCond backreference , which syntax is like %1 , to match grouped parts on the previous RewriteCond.
Also, thanks Jon Lin for noticing that the RewriteCond -f option needs an absolute local path.
Update: Added %{DOCUMENT_ROOT} to RewriteCond -f , found out by Jon Lin

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1