htaccess not working in sub directory - .htaccess

My website is hosted in a sub directory (http://example.com/folder).
In root directory I forward http://example.com to http://example.com/folder where I have created a blog page and I rewrite URL like this:
RewriteEngine On
RewriteRule ^([^/]*)-(.+)\.html$ \/folder\/blog\.php?title=$1&bid=$2 [L]
This is working but it affecting other folder URL means like http://example.com/folder2 who will be redirected to my blog.

You can check if the directory or the file really exist and not apply the redirection with conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)-(.+)\.html$ \/folder\/blog\.php?title=$1&bid=$2 [L]

Related

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.

htaccess, How to redirect one directory to one file, another directory to other file?

I have problems using htaccess in WAMP. I need to do the following:
1) redirect URLs like http://localhost/movie_questions/some_number/... to the file http://localhost/movie_questions.php
2) redirect URLs like http://localhost/movie_quiz/some_number/... to the file http://localhost/movie_quiz.php.
3) It's preferable that URLs in rewrite url rules be relative, because I later plan migrating to an Internet server.
Directories 'movie_questions' and 'movie_quiz' do not exist on my server.
Here is my code:
RewriteEngine On
RewriteRule /movie_questions/(.*) /movie_questions.php
RewriteRule /movie_quiz/(.*) /movie_quiz.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Instead of redirecting, I get 404.
You need to remove the leading slash from your RewriteRule's pattern
RewriteEngine on
RewriteRule ^movie_question/(.*)$ /movie_question.php [L]
RewriteRule ^movie_quiz/(.*)$ /movie_quiz.php [L]

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]

Htaccess redirect with cakePHP

I have a cakePHP app working at http://domain1.com/domain2, and want to point http://domain2.com/ to this application.
I have done this change the document root of domain2 to public_html/domain2, but, when I go to: domain2.com all css, javascript and images are not loaded, with a message saying controller not found.
What I can do?
All domains have a folder with their domains without the TLD at domain1.com, I think if I create a htaccess and get the domain without TLD, and finally change rewriteBase, will work as expected, but don't know how to do this.
Current htaccess of domain2:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
You can try adding an htaccess file in domain2's document root with the following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(css|js|png|jpe?g|gif|bmp|ico)$ [NC]
RewriteRule ^/?domain2/(.*) /$1 [L,PT]
This should remove the domain2 part of the links.

htaccess rules losted when directory exists

this is my code at .HTACCESS file in my web server:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This code works well, but when a directory exists the website redirect to Index of (Apache directory browsing):
For example:
http://www.mywebsite.com/XXXX This redirects to the URL http://www.mywebsite.com/XXXX and the page show information about XXXX. (Directory XXXX doesn't exists)
Directory ZZZZ exists and http://www.mywebsite.com/ZZZZ redirects to Index of (Apache directory browsing)
Finally, the question is how to redirect as 1. example.
RewriteCond %{REQUEST_FILENAME} !-d
This line disables rewriting for existing directories. Simply remove it if you always want to rewrite.

Resources