htaccess subfolders for html site - .htaccess

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.

Related

.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]

Styles and files not working after .htaccess rewrite rule

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

Rewrite html files in root to directory

Hi I'm in need of some assistance.
Problem:
I have now made my site multilingual using a CMS which processes everything at index.php
I am trying to redirect all .html pages being accessed from root,like
http://www.website.com/englishpage.html
I want to redirect it to
http://www.website.com/en/englishpage.html
and then a secondary redirect will dissect that information and send it to index.php which will then serve the correct page.
Right now, I'm getting too many redirect errors
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*\.html)$ en/$1\.html [R=301,L]
RewriteRule ^(en|ru)?(\/)?(.*)$ index.php?c=$1&q=$3 [L,QSA]
what am i doing wrong here
thanks
(.*\.html) is not specific enough and therefore also matches /en/englishpage.html. It also keeps on adding .html to the end.
So /englishpage.html redirect to /en/englishpage.html.html, which redirect to /en/en/englishpage.html.html.html etc.
To fix both problems:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)\.html$ en/$1\.html [R=301,L]
RewriteRule ^((en|ru)/)?(.*)$ index.php?c=$1&q=$3 [L,QSA]
PS no need to escape a /

htaccess issue not working properly

I am using URL Rewriting in .htaccess file my problem is this in root folder I have index.php file and I am redirect to en/home/ folder but redirection is not working when I use this .htaccess code.
RewriteRule ^([^/]*+)$ en/peoples/globalCardDetail.php?p=$1
Rewriting is working fine like this http://www.domain.com/atiq ur rehman. But when I access my domain http://www.domain.com this is redirecting to this page en/peoples/globalCardDetail.php.
You need to change your regular expression from ^([^/]*+)$ to ^([^/]+)$.
Also, you may want to add some conditions:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ en/peoples/globalCardDetail.php?p=$1 [L]

Limit RewriteRule in htaccess file

i got a site php based with some rules in the .htaccess file to get rid of file extentions in the url adress bar. Basically it takes http://netbureau.com.br/en/about.php/ and turns it into http://netbureau.com.br/en/about.
Here are the lines in the htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when i try to access the rss feed of the blog at http://netbureau.com.br/blog/?feed=rss2 and when i try to set custom permalinks for the blog at http://netbureau.com.br/blog. It gets messed up by the htaccess file.
So is there any way to disallow the RewriteRule for the /blog folder so that i can get back my rss link and set custom permalinks in the blog?
I know it's at the same time Wordpress related but it feels more connected to the htaccess file than Wp.
Thanks in advance.
EDIT #1:
I've set up the wordpress permalinks to the default structure which goes like this: http://netbureau.com.br/blog/?p=123 This made my rss link back for good.
The remaining problem is that Wordpress gives me its own rewriterule which is:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Is there a way to to still use the first rule to apply to the whole site except the /blo/ folder and apply the WP rule only to the /blog/ folder?
I've tried different combinations but without luck so far. I could only have the site without the custom links for the blog or the custom blog links and a 404 on the pages of the site.
Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The !^/blog/? expression excludes any URI that starts with /blog

Resources