how sling rewriter works clarification

我与影子孤独终老i 提交于 2020-01-02 09:30:12

问题


I am trying to understand how sling url rewrite works. I'm following this url -

http://www.cognifide.com/blogs/cq/multidomain-cq-mappings-and-apache-configuration/

Steps I've done in publish environment -

/etc/map.publish/http:

 jcr: primaryType: "sling:OrderedFolder",
 home: {
     sling:internalRedirect: ["/content/geometrixx/en.html"],
     jcr:primaryType: "sling:Mapping",
     sling:match: "localhost:4503/$"
 },
 localhost.4503: {
     sling:internalRedirect: ["/content/geometrixx/en"],
     jcr:primaryType: "sling:Mapping",
     redirect: {
         sling:internalRedirect: ["/content/geometrixx/en/$1","/$1"],
         jcr:primaryType: "sling:Mapping",
         sling:match: "(.+)$"
     }
 }

1) However, when I hit this url :

 http://localhost:4503/products.html then I got 404 error. 

2) Moreover, I want to implement when user hit on this url :

  http://localhost:4503/content/geometrixx/en.html then it should open 

  http://localhost:4503/en/products.html. 

Please let me know is it possible by following the above approach

Update: I'm trying to access through dispatcher. I'm using Apache 2.0 on windows 7, CQ5.6.0. My httpd.conf looks like below -

 <IfModule disp_apache2.c>
 DispatcherConfig conf/dispatcher.any
 DispatcherLog    logs/dispatcher.log
 DispatcherLogLevel 3
 DispatcherNoServerHeader 0
 DispatcherDeclineRoot 0
 DispatcherUseProcessedURL 0
 DispatcherPassError 0
 </IfModule>
 <VirtualHost *:80>
   ServerName localhost
   DocumentRoot "C:/Apache2/htdocs/content/sitea"
   RewriteEngine On
   RewriteRule ^/$ /content/geometrixx/en.html [PT,L]
   RewriteCond %{REQUEST_URI} !^/apps
   RewriteCond %{REQUEST_URI} !^/content
   RewriteCond %{REQUEST_URI} !^/etc
   RewriteCond %{REQUEST_URI} !^/home
   RewriteCond %{REQUEST_URI} !^/libs
   RewriteCond %{REQUEST_URI} !^/tmp
   RewriteCond %{REQUEST_URI} !^/var
   RewriteRule ^/(.*)$ /content/geometrixx/en/$1 [PT,L]
    <Directory "C:/Apache2/htdocs/content/sitea">
     <IfModule disp_apache2.c>
           SetHandler dispatcher-handler
           ModMimeUsePathInfo On
         </IfModule>
         Options Indexes FollowSymLinks MultiViews
         AllowOverride all
         Order Allow,Deny
         Allow from all
 </Directory>
 </VirtualHost> 

3) Now, when I hit : localhost/content/geometrixx/en/products.html then I get the page and dispatcher also cache the page. But after if I navigate to any page for example Products ->Triangle then URL becomes localhost:4503/products/triangle.html due to Sling mapping. is this expected? As dispatch does not know about Sling mapping hence it does not cache triangle.html. How to make dispatcher cache work?

4) As the rewriter rule is there(RewriteRule ^/(.*)$ /content/geometrixx/en/$1 [PT,L]), if I hit this url localhost/triangle.html then I should get the proper page as localhost/content/geometrixx/en/triangle.html but I get 404 error.


回答1:


I have used these mapping on CQ 5.6.1 and they seem to work. Please find JSON exported from my instance:

{
  "jcr:primaryType": "sling:OrderedFolder",
  "home": {
    "sling:internalRedirect": "/content/geometrixx/en.html",
    "sling:match": "localhost.4503/$",
    "jcr:primaryType": "sling:Mapping",
  },
  "localhost.4503": {
    "sling:internalRedirect": "/content/geometrixx/en",
    "jcr:primaryType": "sling:Mapping",
    "redirect": {
      "sling:internalRedirect": [
        "/content/geometrixx/en/$1",
        "/$1"
      ],
      "sling:match": "(.+)$",
      "jcr:primaryType": "sling:Mapping",
    }
  }
}

The only change I've made is the port separator in the first sling:match column - I have changed it form colon to a dot. Just to be sure we're working on the same configuration, I've created a CQ package containing my config.

This configuration makes 3 things:

  1. When user requests http://localhost:4503/ they will be redirected to /content/geometrixx/en.html
  2. When user requests http://localhost:4503/products.html (or any other page from the /content/geometrixx/en subtree) they will be redirected to /content/geometrixx/en/products.html.
  3. All paths in <a>, <img> and <form> tags will be mapped to their short version, eg.:

<a href="/content/geometrixx/en/products.html">Products</a>

will be rewritten to

<a href="/products.html">Products</a>


About your second question - Sling mappings doesn't allow to redirect user to the mapped version of the URL. Geometrixx site uses BrowserMap library, which (amongst other things) redirect user to the shortened version of the URL using JavaScript. That's why entering following URL:

http://locahost:4503/content/geometrixx/en/products.html

will redirect you to to /products.html a second after the page is loaded.



来源:https://stackoverflow.com/questions/21662780/how-sling-rewriter-works-clarification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!