RewriteRule for Subdirectory - php

I'm new to the Rewrite mod and have been looking over it for the last few days but I am still struggling to get this to work.
I already have a .htaccess file in the root directory that is forcing the secure version of the site and hiding extensions, so that https://www.example.com/help.php goes to https://www.example.com/help
My problem is my admin tool at https://www.example.com/admin
I'd like it to work so that a page like list.php appears as https://www.example.com/admin/list but no matter what I trial, it always either breaks or redirects to the root index file.
Here my root .htaccess file
FallbackResource /index.php
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Can anyone help with what I should add to the admin directories .htaccess file, it's a Redhat server if that helps.
Thanks in advance for any help.

Related

.htaccess started redirecting images

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]

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!

htaccess mod-rewrite: link to subdirectory from root - neglect dirs in subdirectory?

I asked part of this question a while ago on stackoverflow and this works perfect:
es
I have a multi-domain-server each directory is correspondend to a domain-name. However my main-domain name www.mydomain.com is also a directory on my server and not linked to automatically.
This is why I use this in my root of the website …
root/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
Now when I call www.my-website.com it automatically runs the /my-website directory. That's just fine. However in this directory I have another .htaccess file with this in it.
root/my-website/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^/jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(/downloads/.+)/?$ assets/$1 [L,NC]
So this means that for this website some links like /projects/ are just "neglected" and don't actually exist.
However if I run this website now live on my server and due to the main-htaccess file in my root, the RewriteConditions in my sub-directory don't work and I get a "Not found"
How can I make this work together?
Have it this way:
DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
/my-website/.htaccess:
RewriteEngine On
RewriteBase /my-website/
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/my-website/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I was posting this on serverfault, when question was deleted. You already have a working answer, but I thought I'll still post this here, so you can have some explanations about this:
When you have mod_rewrite instructions in a .htaccess in a subfolder, only those instructions will apply, those of the parent folder will not be applied. You can make the rules that are in the .htaccess of the parent folder apply by using the
RewriteOptions Inherit
directive in the subfolder. But then it could become a bit messy. And you need to remember that the rules in the parent folder are applied after the rewrite rules in the subfolder. Better you go with a solution like proposed in only 1 .htaccess file.
Your 3 last RewriteRule search for a "/" at the beginning, which they never have in a per directory context, but have in a per server context. So if you are in .htaccess file (per dir context) then the comparison is made with a string that has no / at the beginning. See the doc about the RewriteRule.

URL Rewriting domain to use subdirectory as root; still shows that directory in URL in Analytics?

I set up a certain domain to rewrite into a folder, shown below, but have been having people show up in that folder in Google Analytics.
in short: /c/ -> root when from domain, but GA still shows domain/c/ in Visitors Flow
root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt
Options -Indexes
and /c/'s .htaccess is just an edited html5boilerplate one with the only difference being the same php and txt rewrite above.
But on Google Analytics, more than half of my visitors are still ending up on /c/index.php.
Visitor Flow: http://i.imgur.com/JwtcTFI.png
I haven't seen /c/ in my URL, so I really don't have a clue what's going on.
Any ideas? I do use relative URLs (is this a bad idea?) so that causes the flow to continue in the /c/ folder.
Thanks for the help!
Your rules do nothing to prevent people from directly accessing /c/whatever. If you want to prevent that, you need to add an extra rule:
RewriteCond %{THE_REQUEST} \ /c/
RewriteRule ^c/(.*)$ /$1 [L,R=301]
Oo, I found the answer! It seems like www.domain was redirecting to domain/c/index.php. I had a rewrite rule to remove the "www" in the /c/.htaccess, but not the root .htaccess.
I fixed it by adding code to remove the "www" in the root htaccess too BEFORE the domain>>directory rewrite. (after didn't seem to work)
I added
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
above
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
(in the root htaccess)
edit: It also probably has to do with the (www.), I dropped that due to redundancy. Now it's just RewriteCond %{HTTP_HOST} ^certain-domain.com$ [NC]
I also added a rewrite for /c/ to root in the /c/ .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /c/
RewriteRule ^(.*)$ /$1 [L,R=301]

Rewrite URL with .htaccess to hide PHP extension

I've a question, I'm developing a webservice for a specific app.
Last week I transferred my back-up from server1 to server2, since that moment my .htaccess won't work anymore.
The url for my webservice is:
http://apps.holtesdesign.nl/inholland/apiv0/
This url works fine, to load the bugreport module:
http://apps.holtesdesign.nl/inholland/apiv0/bugreport.php
But the url was supposed to be:
http://apps.holtesdesign.nl/inholland/apiv0/bugreport
When you'll open the last url, you'll see that it will result in an internal server error.
Do you know how it's possible?
My .htaccess looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The server is running on CentOS, and I've already checked if mod_rewrite is functioning...
I hope you can help me guys!
Cheers,
Jelmer
This will fix it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
If it doesn't work correct the settings of your Wamp Server:
Left click WAMP icon
Apache
Apache Modules
Left click rewrite_module.
This will hide the trailing slash and hide .php in root & sub directories.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Working Concept for me:
# Options is required by Many HostSevice
Options +MultiViews
RewriteEngine on
# For .php & .html URL's:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Use this code in Root folder of your website in .htaccess File : Example
offline - wamp\www\YourWebDir
online - public_html/
Important Note : If it doesn't work correct the settings of your Wamp
Server: 1) Left click WAMP icon 2) Apache 3) Apache Modules 4) Left
click rewrite_module
try this.. it works! :)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Categories