.htaccess Issue: JS and CSS Rending index.php - .htaccess

I'm using a WAMP installation and routing everything through my index.php file. The CSS and JS files in my header get called just fine when I remove the .htaccess file, so the file paths are correct, but once I insert the .htaccess file again Chrometools throws this error upon including my JS file
Uncaught SyntaxError: Unexpected token <
When viewing Chrometool's Network>>Response, it shows the JS file contains the content of everything rendered by the index.php file.
Here's my code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]
I thought the %{REQUEST_FILENAME} !-f was supposed to prevent any rewriting of a file when it's called directly?
UPDATE
I've found the line causing the issue is is the second RewriteRule.
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
Because my JS and CSS are stored two directories deep from my project root, the rule is directing the file to my index.php.
Neither of these below commands have any affect until I comment out the RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
Any ideas?

Try and change this line.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
To this
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
See of that works for you.
Edit:
You need the conditions for all your rules. Rewrite Conditions only applies to the first rule following it. Also you can just have it so if it matches static content, then just not do anything. Try your rules this way.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
RewriteRule ^(.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]

Related

.htaccess file prevents images from loading

I got an .htaccess file which I use to rewrite my urls.
RewriteEngine On
RewriteRule ^configurator/(.+)$ configurator.php?pc=$1 [QSA,L]
RewriteRule ^product/(.+)$ product.php?product=$1 [QSA,L]
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
Inside my configurator my images are not loading. If I disable the .htaccess file and I go the the path of my images they load.
How can i make my .htaccess file not block image loading?
With your shown attempts, please try following. make sure to clear your browser cache before testing your URLs. Also make sure that your htaccess rules file is present in same folder where folders (configurator, product) are present.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^configurator/(.+)/?$ configurator.php?pc=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(.+)/?$ product.php?product=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?page=$1 [NC,QSA,L]

404 errors after url rewrite with .htaccess

I'm using url rewriting in my website.
I've these url:
works
example.com/_new/app/planning
example.com/_new/app/user_info
doesn't work (404)
example.com/_new/try
example.com/_new/features
Here my rules:
RewriteEngine On
# Removing extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# example.com/app/[module]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?param1=$1 [L]
# example.com/app/[module]/[action]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2 [L]
# example.com/app/[module]/[action]/[type]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3 [L]
# example.com/app/[module]/[action]/[type]/[id]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3&param4=$4 [L]
Here the file structure:
/_new/
/_new/try.php
/_new/login.php
/_new/app.php
The try.php and index.php should be shown as example.com/try or example.com/login.
The app.php loads module from another directory. This one should be:
example.com/app/module_name.
What's the problem ?
Thanks.
In htaccess, the base URL example.com will be skipped in the matching, and in your file, the first regex ([^\.]+) will match /_new/try and it tries to redirect you to a file /_new/try.php which does not exist, hence 404.
Make sure to test your regex before trying to use the URL, you can benefit from https://regex101.com/ to make your tests.

Using an IF statement in a HTACCESS ReWrite rule

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
the above i have in my htacces file which rewrites directory/page to index.php?id=directory/page
thats working fine.
I also want to be able to add the following to it:
domain.com/sections/page rewrites to index.php?section=1&id=page
domain.com/sections/page2 rewrites to index.php?section=1&id=page2
domain.com/page rewrites to index.php?id=page
the ID is going to be different for each page
You have to take a look at RewriteCond and RewriteRule directives.
That's a sample .htaccess based on your edit.
RewriteEngine On
# This will process the /sections/(.*) requests, ?section=1 will be appended to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]
# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]

rewrite to another file if .php file not exists

I'm using the following rule to serve the associate .php file
All last directory pathname will go to the associate .php file
Ex. /about/ or /about will go /about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php`
So when I use http://example.com/abc it points to /abc.php, this is working fine. But if /abc.php file does not exist in that directory then I like to forward that to /listing.php file. How can I do that? please help
Also, I need this to apply to the root directory only. means
http://example.com/..*here*.. not for any subdirectory.
Thanks
Try this, this should be tested:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0\.php !-f //check if (for example) abc.php not exists then
RewriteRule ^(.*)$ /listing.php
RewriteRule ^(.*)$ $1.php
Update: I tested this on my local server, note that: the folder is test you can adjust for your own
Options All -Indexes
RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond test\/$0\.php !-f
RewriteRule ^(.*)$ listening.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

htaccess code for custom directory rewrite

right now i currently have this script that's working
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1
and this does exactly what i want. it displays:
example.com/userid
instead of
example.com/profile.php?username=userid
but now im trying to implement another aspect like this. im trying to have:
example.com/c/1234
instead of
example.com/c/postpage.php?link=1234
the directory seems to be messing with my .htaccess code and i cant even get close to it working.
any advice is welcome.
Try to change the source of your .htaccess file with this code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /profile.php?username=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^c/([^/]+)$ /c/postpage.php?link=$1

Resources