.htaccess wildcard subdomain pointing to directory + hash

Asked

Viewed 279 times

2

Very simple to doubt. Redirect by . htaccess containing a wildcard and hash subdomain.

Of:

http://carros.domain.com/visualizar#123
http://casas.domain.com/visualizar#345
http://aps.domain.com/visualizar#567

To:

http://domain.com/carros/ver#123
http://domain.com/casas/ver#345
http://domain.com/aps/ver#567

My biggest problem is changing the "view" to the "view".
I tried countless ways and failed in the mission.

Something approximate I found here in the stack:

 RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/visualizar
 RewriteRule ^(.*)$ http://domain.com/%1/ver/$1 [L,NC,QSA]

EDIT:

Solution that worked based on the answers of these beautiful people:

   RewriteCond %{HTTP_HOST} carros\.domain\.com
   RewriteCond %{REQUEST_URI} visualizar([^#]+)?([#\d\w\s]+)?
   RewriteRule (.*) http://domain.com/ver%1%2 [R=301,L]

I repeated that to all my subdominios.

  • So show me what you tried.

  • I added the example I’m working on

  • You can explain better if this has to work with multiple domains or if parts like "Domain.com", "cars" are fixed?

  • No, the domain is fixed. Already the "cars" is wildcard. I gave an improved in the examples. See if I was clear.

2 answers

2

This rule redirects all subdomains:

   RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.foo\.bar$
    RewriteRule ^/?(.*)$ http://www.foo.bar/$1 [R=301,L]

Based on that, your case would look like this

   RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.foo\.bar/visualizar$
    RewriteRule ^/?(.*)$ http://www.foo.bar/carros/ver$1 [R=301,L]
  • I worked on your example, I couldn’t. I improved my example. Thank you!

1

Using the possibility of regex having numbered capture groups I think this might work:

RewriteEngine on
RewriteRule ^https?:\/\/([^\.]+)\.domain\.com\/([^#]+)([#\d]+)$ http://domain.com/$1/ver$3 [R=301,L]

Based on regex behavior like this: https://regex101.com/r/qG3vO5/1, the idea is to capture 3 groups and regroup 2 of them in rewrite.

  • Look, I didn’t know you could use it right on Rule. I used your example and left the capture groups as an option. Well, I tested on localhost but unsuccessfully: RewriteRule ^https?:\/\/([^\.]+)\.localhost\/([^#]+)?([#\d]+)?$ http://localhost/$1 [R=301,L]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.