Home rewritting url for replicated sites using htaccess
Reply: 2

rewritting url for replicated sites using htaccess

drr
1#
drr Published in 2018-01-10 19:19:02Z

I'm having a problem rewriting the URL of a replicated site.

a replicated site has this format: https://domainname/sitename

so if you go into a subdirecotry in the system, url should look like this:

https://domainname/sitename/sub1

What I've tried so far is doing this:

RewriteCond %{REQUEST_URI} !^(.+)/index(.+)$       
RewriteRule ^(.+)/(.+)$ index.php?_route_=$2 [PT,L] 

which solves this format: https://domainname/sitename/sub1. But after putting up that rule it no longer accepts this https://domainname/sitename

any advice here?

masterfloda
2#
masterfloda Reply to 2018-01-10 22:37:14Z

The regex in your RewriteRule expects a string followed by a slash followed by a string, which won't match sitename. You can change the regex to check for characters until the first slash and then an optional string (including slashes) after that:

RewriteRule ^[^/]+(/(.+))*$ index.php?_route_=$2 [PT,L]

See the explanation on regex101

The values for $_GET['_route_'] will be:

  • https://domainname/sitename => (empty string)
  • https://domainname/sitename/sub1 => sub1
  • https://domainname/sitename/sub1/sub2 => sub1/sub2

if you also want to pass the sitename, you need to put it in a capturing group, which will increase the group index:

RewriteRule ^([^/]+)(/(.+))*$ index.php?_site_=$1&_route_=$3 [PT,L]
Rich Bowen
3#
Rich Bowen Reply to 2018-01-11 15:12:38Z

It is also recommended that you just replace that entire ruleset with

FallbackResource /index.php

And then add the smarts to index.php to parse PATH_INFO

This we'll be more efficient and customizable.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331191 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO