Question

Story: I'm developing a website which has three sub folders in the main directory as /a/, /b/ and /c/. Contents in site.com, site.com/a/, site.com/b/ and site.com/c/ are different; however, the codes and files are completely similar. In order to reduce the volume of the codes, I used the following .htaccess rule and deleted the similar codes in /a/, /b/ and /c/ subfolders:

RewriteRule ^(a|b|c)/(.*) $2?folder=$1 [L,QSA]

Everything works fine, but below is the only problem. Problem: I have a search button in the menu bar of all four pages (i.e. site.com, site.com/a/, site.com/b/ and site.com/c/). When I search a sentence like "hello world" in the subfolder pages (i.e. site.com/a/, site.com/b/ and site.com/c/), the results return in the following format: site.com/hello-world/?folder=a site.com/hello-world/?folder=b site.com/hello-world/?folder=c But, above search results must return in the below format instead: site.com/a/hello-world/ site.com/b/hello-world/ site.com/c/hello-world/ Is there any .htaccess rule to fix this problem?

Was it helpful?

Solution

You can use this rule to 301 redirect site.com/hello-world/?folder=c => site.com/a/hello-world/

RewriteCond %{THE_REQUEST} \s/+([^?]+)\?folder=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]

Just make sure this is placed before your existing rule

OTHER TIPS

You cannot fix the issue in .htaccess. You need to fix your application so that it generates correct URLs for the search results. Currently it is using the current page URL when generating search results, and that is why you get results like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top