is it possible to make rule that works like this:
example.com/param1val/param2val/param3val/...
And as follow, many params. I can't know how many of them would be there, but I want to rewrite it to:
example.com/index.php?param1=param1val¶m2=param2val¶m3=param3val...
and so on. Is it possible in one rule?
As per my comment this answer is for case 1):
RewriteEngine On
## reucrsion based rule to convert /user/n1/v1/n2/v2 to /user.php?n2=v2&n1=v1
RewriteRule ^(index\.php)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]
Related
I've a simple mod_rewrite rule
RewriteRule ^product/([^/.]+)/([^/.]+)/?$ product.php?partNumber=$1&partName=$2 [L]
It works great for 99.99% of products but there are 3 or 4 products which have a forward slash in their part number (eg PART001/1)
which rewrites to something like:
/product/PART001/1/part-name-here-for-nice-seo
Obviously this doesn't work as it's looking in an extra directory. I need the part number passed correctly as it's used to look up the index in the database and fetch all the product's information.
Is there any way round this?
Looks like you just need to change the ([^/.]+) grouping in your regex to also match slashes. Try:
RewriteRule ^product/(.+)/([^/.]+)/?$ product.php?partNumber=$1&partName=$2 [L]
I want to rewrite the newsletter page of my site to url.com/newsletter/ the problem is, I have another rule overlapping this step. The rules looks like this :
RewriteRule ^([^/]*)/$ /index.php?categories=$1 [L,QSA] //Primeryrule overlapping the secondary rule
RewriteRule ^newsletter/$ /?newsletter=$1 [L]
Is there any possibility to apply special case rules or something like that (I don't want to use any workaround like .html or .php extension or stuff like this, just the url as above).
Apache reads these rules from the top down. So put your new rule first and then the existing rule and give it a try.
Just found the solution, that I need to keep the newsletter rule on top of the other rule to make it apply first.
I am trying to figure out how to rewrite URLs from something like this:
example.com/collection.php?collection=1
Into:
example.com/collection-name.php
I recently redesigned an e-commerce site and need to redirect the old URLs to the new ones. I've seen loads of instructions on how to use the value of collection=1 in a rewritten URL but not how to use text instead of the value. There are only 5 collection values that need to be redirected but in addition there are also old URLs that have multiple params in them that also need to be rewritten/redirected as text. I hop that made sense.
So I think I can get them all worked out if I can get the initial redirect set up.
Other/more complex old URLs look like this:
example.com/collection.php?collection=1&product=2&item=3
Which would then need to be redirected to:
example.com/collection-name/sub-collection-name/product-name.php
Not sure exactly how to go about doing this. I don't want to write line after line in the .htaccess file but I have no idea of how else to accomplish this.
Thanks in advance, I appreciate andy and all help in this matter!
EDIT------------------------------------
Here's my new rewrite condition and rule based on the info provided to me. This would be the rule for the URLs containing all the query params using 3 separate RewriteMaps.
RewriteCond %{QUERY_STRING} collection=([^&]+)&product=([^&]+)&item=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}/${rangemap:%2}/${productmap:%3}\.php [R=301,L]
Please let me know if anything looks off or I missed anything. Wasn;t sure if I needed to use $1, $2, $3 for each of the query params. I'm still a NOOB with rewrites. Thanks!
Edit------------------------------------------------------
Using the following code in my .htaccess file:
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}\.php [R=301,L]
My URLs that start as "example.com/collection.php?collection=1
Are being rewritten as: example.com/shop/.php?collection=1
I am a bit lost on this one. Could it be that my Redirect Maps are not being read? Or is it something else? Thanks for the help.
You want to look into using a RewriteMap. You can specify the maps you want for collection, sub-collection and products, and then look up the text for each number.
So you would define a RewriteMap in your virtualhost config with something like
RewriteMap categmap txt:/path/to/category/map.txt
Then your rewrite rule would look something like
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php ${categmap:%1} [L,R]
Add more RewriteConds for your more complicated cases and make separate rules out of them. Place the more specific rules first in the file, then the more general ones.
Now my urls look like this:
http://mysite.com/home/index/2
What would be the htaccess rule to turn that into this?:
http://mysite.com/page/2
Note: 2 can be any number starting from 1 to infinity.
I refer to this website for reference every time I need to rewrite the url. You should probably read there too. Below is my suggestion, please modify according to your need.
RewriteRule ^/home/index/(.+) /page/$1 [R]
I am really new to .htaccess. I was wondering how to create a complex seo friendly urls for the search string and selected filters.
I'm using following code currently for the search.
ReWriteRule ^search/(.*)/(.*) ?module=search&q=$1&page=$2 [L]
ReWriteRule ^search/(.*) ?module=search&q=$1 [L]
When it comes to adding filter options it starts to be a nightmare. Consider following filters;
Short by: none, name, date + ASC, DESC.
Category: none, category ID.
Search In: All, title, Message, Author.
When all filters are selected our address would be;
Raw :
www.site.com/?module=search&q=test&shortBy=name_ASC&catID=1&searchIn=title
Seo Friendly : www.site.com/search/test/name_ASC/1/title/
Now that's not complex at all but I just want to understand the how things work.
Do I have to apply all the filters to URL even if they are not selected? This would create longer URLs for no reason so I believe there must be another approach to this.
How do I define the rule in .htaccess? As in my example, I am writing at least 2 rules in my .htaccess file (2nd one is for pagination).
All I could think right now to do something like this ^search/(.*)/(.*) ?module=search&$1=$2 [L] but this doesn't look elegant.
I will be glad if you could help me out with this problem. I will be grateful if you could also share some of your experiences to a .htaccess newbie like I am.
Put the rules below at the top of your .htaccess file (in the root directory of your site) and then select one of the options below, modify it to your needs and place in after in your .htaccess file.
The first option will match the most urls and is not recommended. The second will only match urls with 5 directories, but requires all the paramters, so I recommend the 3rd.
Though the pattern below may not look elegant, it is a featured solution in The Definitive Guide to Apache mod_rewrite
For more details look at the apache docs or some examples or this post
In all cases, I am assuming that the requests go to index.php, so you should modify this to match the actual page.
This section should go at top of .htaccess file for all 3 options
#for all 3 options these 2 lines should be at the top of the .htaccess file
RewriteEngine On
RewriteBase /
Option1
#1 if all parameters are optional, and you page (index.php) can handle blank parameters use
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]
Option2
#2 if all parameters are required use
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?([^/]+)/([^/]+)/$ index.php?module=$1&q=$2&shortBy=$3&catID=$4&searchIn=$5 [L]
Option3
#3 if the module is required and other parameters are optional, then this is better
RewriteCond %{REQUEST_URI} ^/(search|produce_detail|other_modules_here)/
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/$ index.php?module=%1&q=$1&shortBy=$2&catID=$3&searchIn=$4 [L]