Mod_Rewrite Help

Status
Not open for further replies.

BlaZe

Active Member
Veteran
3,985
2009
767
485
Hey,

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.

http://domain.com/page.php => http://domain.com/page/
http://domain.com/category-name1.php => http://domain.com/category/name1/


Details: I have couple of files with the following names & I want them to be shown in the following way.

category-name1.php => category/name1/
category-name2.php => category/name2/
category-name3.php => category/name3/
.
.
.
category-name100.php => category/name100/
category-something.php => category/something/
category-lipsum.php => category/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.
 
13 comments
Your application looks messed up, Which framework are you using or if not who coded this ?

Basically rewrite will be a lot easier, if you could modulate the pages, like category.php?name=name1 , This would be much easier to generate rules and generic rules can be generated.
If you follow up with the current scheme you will need static rewrites dropping load speed.

If you're hell-bent on using the current scheme, quote me in your response and I'll create rewrite for you.
 
Your application looks messed up, Which framework are you using or if not who coded this ?

Basically rewrite will be a lot easier, if you could modulate the pages, like category.php?name=name1 , This would be much easier to generate rules and generic rules can be generated.
If you follow up with the current scheme you will need static rewrites dropping load speed.

If you're hell-bent on using the current scheme, quote me in your response and I'll create rewrite for you.

Well actually its not a dynamic website. Its a static one. I'm using PHP just to include footer & header as they will be same throughout the pages & there are couple of snippets on the page in PHP.

I could do it like:

http://domain.com/category.php?p=name1

I mean I can port all the content from individual name1, name2, etc. files into 1 category.php page.

So, to achieve this, what would be the rewrite rule be ?

http://domain.com/category.php?p=name1 => http://domain.com/category/name1/

Also, this

http://domain.com/category.php => http://domain.com/category/
 
Try this
Code:
RewriteEngine On
RewriteRule ^category/([^/]*)/?$ category-$1.php [L]
RewriteRule ^([^/]*)/?$ $1.php [L]
Though the second rule for http://domain.com/page.php => http://domain.com/page/ may create problems for you as it will redirect even if valid existing directories to a .php page so better would be to list them out in the rules rather then applying for all or to change to different method.
 
Try this
Code:
RewriteEngine On
RewriteRule ^category/([^/]*)/?$ category-$1.php [L]
RewriteRule ^([^/]*)/?$ $1.php [L]
Though the second rule for http://domain.com/page.php => http://domain.com/page/ may create problems for you as it will redirect even if valid existing directories to a .php page so better would be to list them out in the rules rather then applying for all or to change to different method.

Its working great. Just one issue.

http://domain.com/category/ is resulting in this URL: http://domain.com/category-.php

& if I don't end a trailing slash at the end then its perfect like http://domain.com/category results to http://domain.com/category.php

Any workaround for this in the regex ?
 
Try this
Code:
RewriteEngine On
RewriteRule ^category/([^/]+)/?$ category-$1.php [L]
RewriteRule ^([^/]*)/?$ $1.php [L]

Its working flawlessly. Thank you very much.
======

Thanks to REAK & Maniac_ for helping too :)

__________________
Added after 1 Day 18 Hours:

Its working, but the index.php is throwing a 404 error and even the style.css file is throwing a 404 error. I'm confused still finding a fix for it.
 
Last edited:
Hmm damn it is even changed index.php to index.php.php and style.css to style.css.php :| Try this
Code:
RewriteEngine On
RewriteRule ^category/([^/]+)/?$ category-$1.php [L]
RewriteRule ^([^/.]*)/?$ $1.php [L]
It won't rewrite if there is a . in the filename so should fix those issues
 
Thanks soft2050 :D

I tried this code & its working fine now. Thanks everyone for the help.

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]
 
Status
Not open for further replies.
Back
Top