I am trying to write a seemingly simple RewriteCond, however, I've been chasing my tail trying to find a solution. I have searched the docs (here, here), in addition to this similar query and this similar query, but haven't been able to solve the issue.
FOLDER STRUCTURE
/config
/public
/js
/css
index.php
/src
/controllers
/ajax
/models
/views
AIM
If the URL is too www.example.com, or any .js, .css, .jng files, include 'public' in the URL.
wwww.example.com -> www.example.com/public/index.php
www.example.com/js/main.js -> www.example.com/public/js/main.js
However, I wish to write a RewriteCond that exempts any other URL request.
wwww.example.com/src/controllers/ajax/doSomething.php -> NO CHANGE
Try this:
RewriteRule ^$ /public/index.php [L,R]
RewriteCond %{REQUEST_URI} \.js$ [OR]
RewriteCond %{REQUEST_URI} \.css$ [OR]
RewriteCond %{REQUEST_URI} \.jng$
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^ /public%{REQUEST_URI} [L,R]
Related
I am trying to do a redirect of a file and folder using .htaccess. I want to add a RewriteCond directive before the RewriteRule to match only example.com/place/sta-ana.html and example.com/place/sta-ana directory. I tried this:
RewriteCond %{REQUEST_URI} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana$1 [L,NC,R=301]
and this:
RewriteCond %{THE_REQUEST} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana$1 [L,NC,R=301]
but both only work for redirecting place/sta-ana.html and not the directory and its files like place/sta-ana/santiago.html and place/sta-ana/santiago/lourdes.html. How can I get the code(s) above to also redirect the sta-ana directory to the santa-ana folder:
example.com/place/sta-ana.html => example.com/place/santa-ana.html
example.com/place/sta-ana/santiago.html => example.com/place/santa-ana/santiago.html
example.com/place/sta-ana/santiago/lourdes.html => example.com/place/santa-ana/santiago/lourdes.html
Looks like you don't need a RewriteCond. Just this rule will do the job:
RewriteRule ^place/sta-ana(\.html|/.*)$ /place/santa-ana$1 [L,NC,R=301,NE]
Make sure this is your topmost rule and you must clear your browser cache completely before testing this change.
I'm trying to achieve a few things through .htaccess ,but keep running into issues. Before you tell me I need to research better and there's already a solution on this or a different forum, please know I've already done that. I always try and figure out things on my own before coming here, but this one is truly stumping me. Everything I've tried has only partially worked. Any help or education here would be truly appreciated.
My site has the following simple structure:
(root)
| index.html
| .htaccess
|
|___portal-folder
| index.php
| home.php
|
|_____admin-folder
| index.php
I'm looking to achieve the following:
When a user navigates to any base directory, for instance site.com or site.com/portal-folder/ they don't see the index file name index.html or index.php in their browser.
Same holds true if the user navigates to the full URL site.com/index.html or site.com/portal-folder/index.php I would like the user to see site.com or site.com/portal-folder/ respectively in their browser.
Strip the file extension off all files in the browser. So for instance navigating to site.com/portal-folder/home.php would show as site.com/portal-folder/home in the browser
The following code I'm using kind of works, but I'm getting strange behavior. For instance:
navigating to site.com/portal-folder/index doesn't remove the index file name and show up as site.com/portal-folder/index instead of site.com/portal-folder/ in the browser
navigating to site.com/portal-folder/ doesn't remove the index file name and shows up as site.com/portal-folder/index.php in the browser.
navigating to site.com/portal-folder/index.php takes the user back to the root site.com
navigating to site.com/portal-folder/home works correctly, but navigating to site.com/portal-folder/home.php doesn't strip the .php extension off.
navigating to site.com works correctly, but navigating to site.com/index.html doesn't remove the index file name.
RewriteEngine On
DirectoryIndex index.html index.php
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Server Information: Apache Version 2.4.46
Have it this way:
DirectoryIndex index.html index.php
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file and optionally remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))(?:\.php|\.html)?[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I have a web project with two directories: "core" and "public".
"Core" contains all of the controllers, views, and files required for the Model-View-Controller.
"Public" contains all public files, like js, css, and less, that can be accessed directly.
I have the following .htaccess in the main directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.less|\.coffee)$
RewriteRule ^$ core/ [L]
RewriteRule (.*) core/$1 [L]
</IfModule>
However, it still rewrites those files to the 'core' directory.
What am I doing wrong?
I recommend you use this instead:
RewriteEngine on
# First, check if a specific type is being requested
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|jpeg|bmp|css|js|less|coffee)$ [NC]
# Second, check if the request is for an existing file
RewriteCond %{REQUEST_FILENAME} -f
# If both conditions are true, then skip rewriting
RewriteRule ^ - [L]
# Otherwise, continue:
RewriteRule ^$ core/ [L]
RewriteRule (.+) core/$1 [L]
The reason your assets were being sent to core is because the conditions only work for the first rule, which was for your application index. Using this method tells mod_rewrite to skip rewriting if an asset is being requested. Once it does that, it can continue with all other rules.
So I have a bunch of .htaccess rules for my angular application and I'm looking for the behavior:
If dist/frontend.html exists, route everything to that. If not route to frontend.html.
This is what I currently have. The rules work as long as I'm requesting a sub route (e.g. http://www.domain.com/404/) but if I'm requesting the root, with no path it's like these rules are never executed and it just relies on the DirectoryIndex.
DirectoryIndex index.html index.html frontend.html index.php
# Rewrite everything which looks like it should be handled by angular here, 404 if a specific resource
RewriteCond %{REQUEST_URI} !\.(jpg|png|jpeg|gif|css|js|html)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/dist/frontend.html -f
RewriteRule ^ dist/frontend.html [L]
RewriteCond %{REQUEST_URI} !\.(jpg|png|jpeg|gif|css|js|html)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/frontend.html -f
RewriteRule ^ frontend.html [L]
Problem is this condition:
RewriteCond %{REQUEST_URI} !\.(jpg|png|jpeg|gif|css|js|html)$ [NC]
Since you're ignoring .html extension and also using index.html as default handler using DirectoryIndex causing both of your rules to be skipped.
If you're going to use one of the frontend.html using one of the 2 rules then you don't really need DirectoryIndex directive, just comment it out and it should work.
I'am struggling with this for a couple of days.
Folder structure is:
index/files/pages/
In main folder (index) there is index.php - this is the only index.php file in whole page.
In files folder there are php files that are actually pages of the website.
In files folder there are couple of more folders (pages) that hold different pages - I just grouped the php fiels into folders to keep it tidy.
I am trying to achivie this.
When I enter url like index/files/pages/somename.php - I want to get 404 or whatever
When I enter url like index/files/somename - this gets rewritten to index/files/pages/somename.php, but the address looks clean.
Now whatever I do I can't get both rules to work, either one overwrites another or nothing works, I get 500 all the time.
Here is my rule that actually works:
RewriteRule ^([-0-9a-z]+)$ pages/$1.php - this is the scenario 2 and its ok.
Now when I add rule that handles direct access to php files, the previous rule is also affected and gets overwritten and everything throws 404, 500 etc. Flags like [L] etc don't have any effect, changing the order also does nothing.
Also I want to forbid direct access to index.php and redirect to index folder (and show clean url)
Here are some additional rules i tried, but no result. As I said earlier setting flags give no results.
RewriteCond %{REQUEST_URI} ^.*\/pages\/.*\.php$
RewriteRule ^(.+)$ [R=404,L]
RewriteRule ^([-0-9a-z]+)$ pages/$1.php
RewriteCond %{REQUEST_URI} ^(.+)\/index\.php
RewriteRule ^(.*)$ index/ [R=301,L]
RewriteCond %{REQUEST_URI} ^\/(.*)\.php
RewriteRule ^(.*)$ [R=404,L]
Thankx and regards
Try these rules:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ index/ [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} \.php[\s?/] [NC]
RewriteRule ^ [F]
RewriteRule ^index/?$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]