Htaccess redirect a specific subdirectory - .htaccess

My current .htaccess file contains the following lines:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I would like to redirect www.domain.com/category/automotive/used+car+dealers to www.domain.com/category.php?category=automotive&subcategory=used+car+dealers.
Should be dynamic and not a static redirect, as I will have other categories and subcategories? Can anyone show me some light on how to proceed from here?
I have googled, and browsed Stack Overflow for nearly 2 hours, but I'm still unable to come out with a solution.
[Sample 1]
RewriteEngine On
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have tweak abit and is able to redirect to the correct file, but somehow the page loads without css and images. please advise. thankyou.
[Sample 2]
RewriteEngine On
RewriteBase /websitefolder/
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works fine just that the url will become category.php?category=automotive&subcategory=used+car+dealers instead of /category/automotive/used+car+dealers. but i wish to have the latter. please advise

Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^(category)/([\w-+]+)$ $1.php?$1=$2 [R,L]
RewriteRule ^(category)/([\w-+]+)/([\w-+]+)$ $1.php?$1=$2&subcategory=$3 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteBase /websitefolder/
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Just remove the R flag from :
RewriteRule ^category/([\w-+]+)/([\w-+]+)$ category.php?category=$1&subcategory=$2 [R,L]
It will not change the URL in the browser.

Here's what should fit your exactly your need:
RewriteEngine On
RewriteBase /
RewriteRule ^category/([a-zA-Z0-9]+)(/([a-zA-Z0-9]+))?$ category.php?category=$1&subcategory=$3 [QSA,NC,L]
Now if you want something more generic, i.e.:
www.domain.com/category/automotive/used+car+dealers
=>
www.domain.com/category.php?category=automotive&subcategory=used+car+dealers
and
www.domain.com/title/automotive/used+car+dealers
=>
www.domain.com/title.php?title=automotive&subcategory=used+car+dealers
Please see ThinkingMonkey's answer it's better than mine :).

Related

htaccess URL Rewrite for blog

I'm having an issue trying to get my htaccess to work properly
I'm custom building my website, but have not figured out how to get the blog posts to show right.
My base URL is working for the pages. Example: mywebsite.com/contact
But I'm trying to get the blog posts to work like this. mywebsite.com/posts/post-title-here
Here is my htaccess, and I've commented out methods I've tried. But not fully understanding what I'm doing wrong. Can someone point me in the right direction?
#Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) index.php [QSA,NC,L]
#RewriteRule ^posts/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ view-post.php?id=$2
#RewriteRule ^posts/([A-Za-z0-9-]+)/?$ view-post.php?slug=$1 [NC,L]
#RewriteRule ^posts/(.*)$ view-post.php?slug=$1
#RewriteRule ^posts/(.+)$ view-post.php?title=$1 [L]
Options -Indexes
Post this above your index.php line in .htaccess
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/?posts/(.*) view-blog.php [NC,QSA,L]
You can try these on your htaccess file:
RewriteRule ^posts/([^/]*)$ view-post.php?slug=$1 [QSA,L]
RewriteRule ^posts/([^/]*)$ view-post.php?title=$1 [QSA,L]

What is the simplest way to remove .html with an htaccess file?

So I have a been trying a lot of different htaccess codings to clean up my url
but am not even able to simply remove .html without something going wrong
Code Im using:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
CODE Im hoping to clean up:
<area shape="poly" coords="3,612,78,557,118,627,5,708" href="collabwitus.html" />
Little new to htaccess and would love some help!
I am doing everything from file manager via hostgator so framing an answer in a more step by step version using file manager would be great!
Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This code worked for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Rewrite rules overriding

I have two rewrite rules which, in what ever order I add them, fail to work together
The first one takes a name from url e.g.
http://myUrl.com/JohnSmith
and forwards it to:
http://myUrl.com/pages/gf_profile.html?user=JohnSmith
Code is:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ /pages/gf_profile.html?user=$1 [NC,L]
On it's own it works fine. The second one is wordpress, I have urls e.g.:
http://myUrl.com/blog/my-blog-post
And it uses:
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
With the second section in, when I try the first URL it thinks it's a blog title and therefore cannot find it.
Is there a way to use them both?
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^([^\.]+)$ /pages/gf_profile.html?user=$1 [NC,L]
And I assume your other rules are in the /blog/ directory (otherwise it doesn't look like it'll work right).
Answer:
Thanks to #anubhava - I simply created a .htaccess file in the /blog/ folder with the following on and all works perfectly now.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I'm not sure why wordpress doesn't add this on install, it should IMHO - hopefully this might help someone who has a similar issue down the line.

decide page based on number of parameters - htaccess url rewrite

i am writing RewriteRule(s) on htaccess which decides which page it redirect to based on the number of parameters.
for example, "http://mysite.com/tommy"
this will redirect to "mysite.com/user.php?user=tommy"
whereas, "http://mysite.com/tommy/pets"
will redirect to "mysite.com/show.php?user=tommy&category=pets"
note, the first URL has only one parameters and it redirects to user.php and second URL has two parameters and it redirects to show.php instead.
please help, and thanks in advance!
Currently...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/?(.*)$ category.php?userId=$1&category=$2 [QSA,L]
RewriteRule ^(.*)$ user.php?userId=$1 [QSA,L]
</IfModule>
Done, for anyone else who need this too...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?userId=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?userId=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ category.php?userId=$1&category=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ category.php?userId=$1&category=$2 [L]

htaccess problem

I given htaccess like this
Options +FollowSymlinks
RewriteEngine On
RewriteBase /xxx/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /xxx/folder/index.php [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]
but giving the url like this
http://domain.com/faq
I get the page variable as index.php. I want to get faq in $_GET['page'].How can i get this?
Remove
RewriteBase /xxx/folder/
Modify
RewriteRule ^(.*)$ index.php?page=$1 [L]
to
RewriteRule ^(.*)$ xxx/folder/index.php?page=$1 [L]

Resources