rewriterule to mach "foldername" to "00_foldername" - .htaccess

I have 100 folders each start with a number and underscore like 001_folder1, 002_folder2.
I wrote 100 lines of rewrite code to match 001_folder1 to folder1 and etc.
RewriteRule ^/?mainFolder\/folder1\/$ mainFolder\/001_folder1/index.php [L]
RewriteRule ^/?mainFolder\/folder2\/$ mainFolder\/002_folder2/index.php [L]
.
.
I don't know if its efficient or not, but I need a general rule to match them.
something like:
RewriteRule ^/?mainFolder\/([a-z]*)$ mainFolder\/[(0-9){3}_$1 [NC,L]
Thanks

You can use this single rule to replae your 100 rules:
RewriteRule ^/?mainFolder/folder(1)/$ /mainFolder/00$1_folder$1/index.php [L,NC]

Related

.htaccess Merge 2 rules into one

I want to merge 2 rules into 1. I want to make sure cause this change will affect around 40 rules.
These are my 2 rules:
RewriteRule ^award/(\d+)/?$ admin.php?io=award&award=$1 [L]
RewriteRule ^award/add/?$ admin.php?io=award[L]
Can i merge them like this:
RewriteRule ^award/add|(\d+)/?$ admin.php?io=award&award=$1 [L]
Is this correct?
You can use:
RewriteRule ^award/(?:add|(\d+))/?$ admin.php?io=award&award=$1 [L]
With an empty parameter award for award/add.
Or:
RewriteRule ^award/(add|\d+)/?$ admin.php?io=award&award=$1 [L]
With award=add for award/add.

How to get Apache's mod_rewrite working?

I know it's been asked a hundred times, and I also took my time searching and trying. Somehow I still can't get it to work, only partially.
I have structure of urls like these: (xxxx can be numbers as well)
index.php?mode=xxxxx
index.php?category=xxx
index.php?product=xxxx
index.php?article=xxxxx
index.php?blog&post=xxxxx
The url I'd like to see in the end of all variations:
www.example.com/something
www.example.com/something/something (in the blog&post case)
I also would like to hide index.php at the end.
If only have numbers and nothing at the end, the rules would be:
RewriteEngine on
RewriteRule ^mode/([0-9]+)$ /index.php?mode=$1 [L]
RewriteRule ^category/([0-9]+)$ /index.php?category=$1 [L]
RewriteRule ^product/([0-9]+)$ /index.php?product=$1 [L]
RewriteRule ^article/([0-9]+)$ /index.php?article=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)$ /blog&post=$1 [L]

Mod Rewrite multiple parameters

I have these rules:
RewriteRule ^farbe-([^-]*)/$ ?farbe=$1 [L]
RewriteRule ^marke-([^-]*)/$ ?marke=$1 [L]
So http://playscout.de/hosen/?marke=diesel/ and http://playscout.de/hosen/?farbe=blau/ becomes http://playscout.de/hosen/marke-diesel/ and http://playscout.de/hosen/farbe-blau/
It works, but i need rules for multiple parameters like http://playscout.de/hosen/marke-diesel/farbe-blau/ so i tryed this for farbe:
RewriteRule ^farbe-([^-]*)/$ &farbe=$1 [L]
Because from second delimeter it have to be &
But it doesn't work. Any suggestions?
Put rule in top, to be executed first:
RewriteRule ^marke-([^-]*)/farbe-([^-]*)/$ ?marke=$1&farbe=$2 [L]

Mod RewriteRule that matches numbers 1-15

I'm using the following 3 rewrite rules to load static cached files if the user is on page 1-15. Is there a way I can combine these, particularly the last 2 into 1 rule? I've been unable to find any way to specify a range of 1-15.
RewriteRule ^$ app/webroot/cache_html/cache_static_popular_results_1.php [L]
RewriteRule ^popular/page:([1-9])$ app/webroot/cache_html/cache_static_popular_results_$1.php [L]
RewriteRule ^popular/page:(10|11|12|13|14|15)$ app/webroot/cache_html/cache_static_popular_results_$1.php [L]
Is there a way I can combine these, particularly the last 2 into 1 rule
How about
RewriteRule ^popular/page:([1-9]|1[0-5])$ app/webroot/cache_html/cache_static_popular_results_$1.php [L]

.htacces RewriteRule problem

This is working:
RewriteRule ^([a-z]{2}/){0,1}showCategory/([0-9]*)/([0-9]*)(/{0,1})$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
with this URL:
http://localhost/showCategory/590/10
Now I want to work with this too:
http://localhost/showCategory/590/transport/10
The rule I tried:
RewriteRule ^([a-z]{2}/){0,1}showCategory/([0-9]*)/([a-z\-_0-9\+]*)/([0-9]*)(/{0,1})$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
How to change the RewriteRule?
This one works with both URLs
RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)(?:/[A-Za-z0-9_-]+)?/?([0-9]+)/?$ /main.php?id=$2&il[lang]=$1&page=$3 [L]
I also simplified the {0,1} with ? which does the same.
The key is in (?:/[A-Za-z0-9_-]+)?, explanation:
(?: Non-capturing group, so what's in here will not be put in any $n
/ Match the slash
[A-Za-z0-9_-]+ Match any word with letters/numbers/dashes/underscores
)? Close non-capturing group, and the ? means it's optional
Try this rule:
RewriteRule ^([a-z]{2}/)?showCategory/([0-9]+)/[a-z-_0-9+]+/([0-9]+)/?$ /main.php?id=$2&il[lang]=$1&page=$3 [L]

Resources