Styles and files not working after .htaccess rewrite rule - .htaccess

I've just added a simple rewrite rule to my .htaccess file to drop .php from this page http://themeat.in/register.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
but now when I go visit that page without the .php (http://themeat.in/register/) all my styles and files have vanished. When I open up the console I see the page name is being treated as a folder.
This is what the file path should be and was before the rewrite, http://themeat.in/css/styles.css
and this is what it is now,
http://themeat.in/register/css/styles.css
I guess it's got something to do with the trailing slash within the rewrite but I'm totally stumped at how to fix this problem? I need the .php dropped and I'd like to keep the trailing slash.
Any help would be greatly appreciated.
Many thanks,
//C

This is because of the rewritten urls. when the url is example.com/register/ apache thinks /register/ is a directory and appends that in front of all relative urls.
To solve this, You can add the following base tag in head section of your webpage :
<base href="/">
For more info, see this post : Seo Friendly Url css img js not working

Related

.htaccess > redirect all subfolders to root (ignore trailing slash)

My gosh - I hate .htaccess ... and don't understand how the hell anything is working.
I am trying to ignore 'subfolders' and use these as guest identifyer.
Ex:
User types > http://subdomain.domain.com/guest_id
Server sends > http://subdomain.domain.com/index.php
Browser shows > http://subdomain.domain.com/guest_id
My htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
The code works great... unless there is a trailing slash.
In this case, all files are served as 'index.php'.
The main Index.php is showing properly, but all external scripts / css / etc. are served as /guest_id/index.php (thus... do not load).
FYI, there might be a conflict as my 'subdomain' is actually invisibly serving domain.com/event/
I have been trying so many permutations... without really understanding how htaccess work.
Your help will be sooooo much appreciated.
1,000 thanks in advance!
Damien
When you have URL such as http://subdomain.domain.com/guest_id/ and are using relative links for your css/js/images then browser attempts to resolve all links by prefixing them with current path hence src='image.png' is resolved as http://subdomain.domain.com/guest_id/image.png instead of http://subdomain.domain.com/image.png.
You can have a rule to force remove trailing slash at the end of all non-directory URLs.
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also you can add this just below <head> tag of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

php dynamic parameters to url with htaccess

I'm trying to make SEO Friendly url by altering php parameters with slash based url.
I want to make
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20
into:
mysite.com/ToplistName/2021-02-20/2021-02-20
I had success with rewriting url but none of my includes are referring to right directory path and I get no css, js and file paths are broken from links.
This is what I have in .htaccess file now:
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ TopList.php?tl=$1&fr=$2&to=$3
Anyone that can help me get this sorted out inside .htaccess file?
Based on your shown samples, could you please try following.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(\d{4}-\d{2}-\d{2})/(\d{4}-\d{2}-\d{2})/?$ TopList.php?tl=$1&fr=$2&to=$3 [L]
I solved this by doing following:
added to html:
<base href="http://example.com/">
added to .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
#1 Changing Full query
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ TopList.php?tl=$1&fr=$2&to=$3
#2 Changing only first landing page
RewriteRule ^([^/.]+)/?$ TopList.php?tl=$1
this gave me following result:
mysite.com/TopList.php?tl=ToplistName ==> mysite.com/ToplistName
mysite.com/TopList.php?tl=ToplistName&fr=2021-02-10&to=2021-02-20 ==> mysite.com/ToplistName/2021-02-10/2021-02-20

.htaccess Rewrite Rule Understanding Problems

Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]

htaccess subfolders for html site

My htaccess code is not working correctly. Hoping to get some help. It's working perfectly unless I click a link within on of my subfolder pages. Here is my code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I removed the .html and added a trailing slash.
When I click on a link when I am within one of my subfolder pages, it generates a weird URL.
Example: When I am at the URL: http://domainname.com/product/2-jars-500mg-hemp-extract-pain-relief-cream/
and I click on the link: href = '/product/3-jars-500mg-full-spectrum-hemp-extract-pain-relief-cream'
It rewrites the link as /product/2-jars-500mg-hemp-extract-pain-relief-cream/3-jars-500mg-full-spectrum-hemp-extract-pain-relief-cream which causes a 404 error.
I currently online have one htaccess file in the public_html folder
This site is hosted through GoDaddy on an apache server.
I tried adding the same htaccess file into the product folder, but that messes up the rewrite.
Rules looks fine, check in source code in browser your url... maybe it's relative and without slash at the beginning or end of uri ( example product/3-jars-500mg... not /product/3-jars-500mg.../ )
You need href = '/product/3-jars-500mg-full-spectrum-hemp-extract-pain-relief-cream/'
that the rule RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html would be true.

Info with mod_rewrite .htaccess

I have been trying forever to get rid of the .php from my site. I want it to look like example.com/about instead of example.com/about.php. I have it to were it will rewrite it so i can type it in as example.com/about but i want it to were when i click on a link on my website to go to example.com/about it loads the url as example.com/about and not example.com/about.php. Overall i would like it so that whenever someone loads anything on my page the end of the url doesnt have the .php. My .htaccess right now looks like so,
RewriteEngine on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank you.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You wouldn't have to change your site's structure as it simply targets "malformed" URIs in the form of "http://x.com/page" without an extension at the end. Hope I helped!
but i want it to were when i click on a link on my website to go to example.com/about it loads the url as example.com/about and not example.com/about.php
To fix that, you need to change links from your site from example.com/about.php to example.com/about. You can still do a redirect when links "in the wild" still point to the .php URLs, but unless you change the links on your site to remove the extension, your're causing every page load to hit your server twice.
As for the redirect:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.php[\?\ ]
RewriteRule ^ /%1/ [L,R=301]
I'm not sure how your rules to put the php extension back is even working, since you're matching for a trailing slash but when you check the file with -f, you'll have a stray trailing slash in your filename: e.g. /some/path/to/the_file/.php instead of /some/path/to/the_file.php
Try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+?)/?
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]

Resources