调试重定向规则不匹配?

时间:2018-10-16 18:02:59

标签: .htaccess apache2

我要重定向

  

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

使用此规则,但不起作用(使用apache2服务器在本地测试)

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

我有一个working htaccesshttps://w3id.org/cocoon/v1.0

我想为类似的东西添加重定向规则

  

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.

2 个答案:

答案 0 :(得分:0)

将您的规则更改为此并重新测试:

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

您规则的正则表达式[./]+仅匹配1个以上的点或/个字符。

答案 1 :(得分:0)

替代解决方案

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]

但是我更喜欢anubhava的解决方案,不想创建另一个文件夹/文件 只需两行。