1

I want to redirect

http://localhost/cocoon/data/vm/azure/windows-d11v2promo-standard

to

http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/data/vm/azure/windows-d11v2promo-standard

with this rule, but it doesn't work (tested locally with apache2 server)

RewriteRule ^data/([./]+)$ http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/data/$1 [L,R=308]

I have a working htaccess for https://w3id.org/cocoon/v1.0

I'd like to add redirect rules for something like

https://w3id.org/cocoon/data/vm/azure/windows-d11v2promo-standard https://w3id.org/cocoon/data/storage/azure/premiumssd-p4


root@CECS-030929:/var/www/html/cocoon# cat .htaccess
Options +FollowSymLinks
# Turn off MultiViews https://stackoverflow.com/a/25423722/646732
Options -MultiViews

# Directive to ensure *.rdf files served as appropriate content type,
# if not present in main apache config
AddType application/rdf+xml .rdf
AddType application/rdf+xml .owl
AddType text/turtle .ttl
AddType application/n-triples .n3
AddType application/ld+json .json

# Turn off mod_security filtering.
# SecFilterEngine Off
# SecFilterScanPOST Off

# Rewrite engine setup
RewriteEngine On

# code 308 permanent redirect, cacheable,
# The request method and the body will not be altered,
# whereas 301 may incorrectly sometimes be changed to a GET method.
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308

# flag https://httpd.apache.org/docs/2.4/rewrite/flags.html
# L|last If the rule matches, no further rules will be processed
# NE|noescape Do not convert to hexcode equivalent
# OR Combine rule conditions with a local OR instead of the implicit AND

# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

# Homepage
RewriteRule ^$ https://github.com/miranda-zhang/cloud-computing-schema [R=308,L]

# make sure we don't have the file or a directory index to serve
# the rest fails to redirect if we do
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

# Rewrite rule to serve HTML content from the vocabulary URI if requested
# http://localhost/v0.1
RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v0\.1/?$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/index.htm [R=308,NE,L]

# http://localhost/v1.0
RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v1\.0/?$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/index-en.html [R=308,NE,L]

# Rewrite rule to serve JSON-LD content from the vocabulary URI if requested
# curl -H "Accept: application/ld+json" -L http://localhost/v1.0
RewriteCond %{HTTP_ACCEPT} application/ld\+json
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.json [R=308,L]

# Rewrite rule to serve RDF/XML content from the vocabulary URI if requested
# curl -H "Accept: application/rdf+xml" http://localhost/v0.1
RewriteCond %{HTTP_ACCEPT} \*/\* [OR]
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.rdf [R=308,L]

# curl -LH "Accept: application/rdf+xml" http://localhost/v1.0
RewriteCond %{HTTP_ACCEPT} \*/\* [OR]
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.xml [R=308,L]

# Rewrite rule to serve N-Triples content from the vocabulary URI if requested
# curl -LH "Accept: application/n-triples" http://localhost/v1.0
RewriteCond %{HTTP_ACCEPT} application/n-triples
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.nt [R=308,L]

# Rewrite rule to serve TTL content from the vocabulary URI if requested
# curl -LH "Accept: text/turtle" http://localhost/v0.1
RewriteCond %{HTTP_ACCEPT} text/turtle [OR]
RewriteCond %{HTTP_ACCEPT} text/\* [OR]
RewriteCond %{HTTP_ACCEPT} \*/turtle
RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.ttl [R=308,L]

# curl -H "Accept: text/turtle" http://localhost/v1.0
RewriteCond %{HTTP_ACCEPT} text/turtle [OR]
RewriteCond %{HTTP_ACCEPT} text/\* [OR]
RewriteCond %{HTTP_ACCEPT} \*/turtle
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.ttl [R=308,L]

# http://localhost/cocoon/data/vm/azure/windows-d11v2promo-standard
# http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/data/vm/azure/windows-d11v2promo-standard
# https://w3id.org/cocoon/data/vm/azure/windows-d11v2promo-standard
RewriteRule ^data/([./]+)$ http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/data/$1 [L,R=308]

# cat .htaccess
RewriteEngine On
RewriteRule ^data/([./]+)$ http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/data/$1 [L,R=308]

# service apache2 reload
 * Reloading Apache httpd web server apache2                                                                                       *
# wget http://localhost/cocoon/data/vm/
--2018-10-17 06:09:10--  http://localhost/cocoon/data/vm/
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 406 Not Acceptable
2018-10-17 06:09:10 ERROR 406: Not Acceptable.
Mzq
  • 1,796
  • 4
  • 30
  • 65
  • Where is your .htaccess located? Can you show your complete .htaccess in question – anubhava Oct 16 '18 at 18:13
  • It is in dir `/var/www/html/cocoon` – Mzq Oct 16 '18 at 18:29
  • Not Acceptable An appropriate representation of the requested resource /cocoon/data/vm/azure/windows-d11v2promo-standard could not be found on this server. Apache/2.4.18 (Ubuntu) Server at localhost Port 80 – Mzq Oct 16 '18 at 18:37
  • http://localhost/cocoon/data/vm/azure/windows-d11v2promo-standard – Mzq Oct 16 '18 at 18:46
  • same error, even only left 2 lines. – Mzq Oct 16 '18 at 19:11

2 Answers2

0

Change your rule to this and retest:

RewriteRule ^data/.*$ http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/$0 [L,R=301,NC,NE,QSA]

In your rule's regex [./]+ only matches 1+ dot or / characters.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • no, this one doesn't work. dot is `\.`, and should be $1, not $0, and I think brackets `()` are needed. – Mzq Oct 16 '18 at 20:06
  • 1
    No brackets needed,`$0` represents complete matching string in `RewriteRule` pattern, which will be `/data/` and we're using back-reference of same in target. – anubhava Oct 16 '18 at 21:20
  • 1
    Ok, must be chrome catching my redirects, [fiddling with it](https://salferrarello.com/chrome-clear-redirect-cache/) for a while to be sure. this also works. – Mzq Oct 16 '18 at 22:12
0

Alternative solution

root@CECS-030929:/var/www/html/cocoon/data# cat .htaccess
RewriteEngine On
RewriteRule ^(.+)$ http://35.227.26.187:5000/cocoon_v1.0?subject=https://w3id.org/cocoon/data/$1 [L,R=301,NC,NE,QSA]

But I prefer anubhava's solution, don't want to create another folder/file for just 2 lines.

Mzq
  • 1,796
  • 4
  • 30
  • 65