Rewrite non-dynamic pages using htaccess - apache

I've been banging my head since many hours on this and I am unable to come up with a solution.
What I need to get done.
domain.com/page.php => domain.com/page/
domain.com/category1-name1.php => domain.com/category1/name1/
domain.com/category2-name1.php => domain.com/category2/name1/
Details: I have couple of files with the following names & I want them to be shown in the following way.
category1-name1.php => category1/name1/
category1-name2.php => category1/name2/
category1-name3.php => category1/name3/
category2-name100.php => category2/name100/
category2-something.php => category2/something/
category2-lipsum.php => category2/lipsum/
Also, I would like to achieve the following rewrite too
/category.php => /category/
/abcd.php => /abcd/
I'm trying but I can't achieve any of it.
I tried this code:
RewriteEngine On
RewriteRule ^category1/([^/]+)/?$ category1-$1.php [L]
RewriteRule ^category2/([^/]+)/?$ category2-$1.php [L]
RewriteRule ^([^/]*)/?$ $1.php [L]
Its only working for /abcd.php to /abcd/ and its throwing 404 errors on rest of the pages, even index is 404 due to it.

Try the following rules in your /htaccess file :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^.-]+)\.php [NC]
RewriteRule ^ %1/ [NC,R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^-]+)-([^.]+)\.php [NC]
RewriteRule ^ %1/%2/ [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1-$2.php [QSA,NC,L]

Related

Rewrite GET Query String using .htaccess

I want to Rewrite my GET Query String from this -
http://www.example.com/verify.php?key=547b52f2b5d20d258e62de1e27b55c
to this
http://www.example.com/verify/547b52f2b5d20d258e62de1e27b55c
I am using the following rule but it does not seem to work -
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
RewriteRule ^[A-Za-z-]+/([A-Za-z0-9-]+)/?$ verify.php?key=$1 [NC,L]
Use the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /verify\.php\?key=([^\s&]+) [NC]
RewriteRule ^verify\.php$ /verify/%1? [R=301,L]
RewriteRule ^verify/([^/]+)/?$ /verify.php?key=$1 [NC,L]

How to create clean url using .htaccess for multiple parameters?

I try to make clean URLS to my web site.
The desired format is: site/category/num example site/sport/3
Both category and page are optional parameters. So i have three rules. The problem is rule three. It only work in first time.
User enter site - tule 1. OK.
User navigate and pick category. Rule 2... OK.
User navigate and want page 2 page. (rule 3) OK.
User want see more items in page 3 (rule 3) Error..
I got parameter site/category/category/3 and navigatioin fail. Now i dont get why do i get category two times.
My .htaccess file work almost.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /index.php?page=$1 [L]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?category=$1 [L]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ index.php?category=$1&page=$2 [NC,L]
Try with:
RewriteEngine On
RewriteCond %{REQUEST_URI} (\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([0-9]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([^/]+)/?$ /index.php?category=$1 [L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)/?$ /index.php?category=$1&page=$2 [NC,L]

Clean URL in CakePHP

I need help with a clean URL in CakePHP. I've been trying for a while with no luck.
This is the existing htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This is the URL that I'm trying to clean up:
http://example.com/signup/?key=dT33Y%2FeKXCjgeo69XpTs39YV%2F4ZnHht0M5E%2BXk9iDKQ%3D
I would like the URL to look like this:
http://example.com/signup/dT33Y%2FeKXCjgeo69XpTs39YV%2F4ZnHht0M5E%2BXk9iDKQ%3D
Please help. Thank you!
Try this code in /signup/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /signup/\?key=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?key=$2 [L,QSA]

Rewrite rule to handle url exception

I have an app that serves many sites and I am using Apache mod-rewrite to map the
url's like this
http://site1.net/controller
http://site2.net/controller2/another_view
http://site3.net/
http://special_case.net/
maps to:
index.php?url=http://site1.net/controller
index.php?url=http://site2.net/controller2/another_view
index.php?url=http://site3.net/
index.php?url=http://special_case.net/hub
My rewrite rules are:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Which handles all the cases except the special last case, where I need to force
the use of a controller called "hub" when handling a specific domain. I collaborate with others on this project which means I can't do anything about the routing once the index file is called.
Can someone fix my rules so that all the above cases resolve?
You current rules don't seem to add the hostname to the url get-parameter. So I added that to to the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(special_case\.net)$
RewriteRule ^(.*)$ index.php?url=http://%1/hub/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ index.php?url=http://%1/$1 [QSA,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://special_case.net index.php?url=http://special_case.net/hub [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

htacces rewrite url - remove folder name from url

I'm tryng to make an htaccess url rewrite for this cases:
www.website.com/index.php/admin/something => www.website.com/admin/something
www.website.com/index.php/website/something => www.website.com/something
www.website.com/index.php/login/something => www.website.com/login/something
note: "something" can be "something1/something2/something3" or "something1/something2" or "something"
I can cut "index.php" with:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
but I can't found a way to remove only "website/" and not "admin/" and "login/" leaving the rest of url.
If taking this approach you will probably have to specify each possible input reference that should not be mapped to your 'website' controller and remap it to index.php/$1 and treat every other request like it is a method of the website controller, mapping it to index.php/website/$1
Something like the below will map anything starting with admin or login to index.php and everything else to the website controller:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^admin.*
RewriteCond %{REQUEST_URI} ^login.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}
RewriteRule ^(.*)$ /index.php?/website/$1 [L]