问题
I am trying to rewrite all URIs inside directory /docs to /docs/index.php, using apaches mod_rewrite module.
The URI doesn't exist as a directory structure, which I don't think is necessary when you are rewriting the URI to something that exit.
I am getting the error:
"The requested URL /docs/view/my_doc was not found on this server."
These are my rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
I am doing the configuration in httpd.conf. Also, I am using a multi v-host setup.
What's my problem and how do I fix it?
I'm using Apache/httpd on CentOS 6.2
UPDATE
Including my VirtualHost as well:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.smicloud.org
ServerAlias *.smicloud.org
VirtualDocumentRoot /var/www/html/www.smicloud.org
ExpiresActive On
ExpiresDefault "access plus 14 days"
# insert logging config, anything else you need..
#Define FileEtag so it doesnt use node
FileETag MTime Size
<Directory /var/www/html/>
Order Allow,Deny
Allow from all
# Get rid of this if you need to allow htaccess files:
AllowOverride None
</Directory>
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
</VirtualHost>
回答1:
This should work
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
回答2:
Ok, there were a couple of things that I had to do in order to fix it, using @donalds suggestion (which I actually tried before):
1 - I had to have a '?' at the end of the regex of RewriteRule:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
Then I can chose whether to have a '/' or not at the end of the address.
2 - I had to add a '/' to the beginning of the regex of my RewriteRule. I.e.:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
3 - I also had to add the 'http://%{HTTP_HOST}/' part to the new URI:
http://
%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
This is what I ended up with:
RewriteRule ^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
http://
%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
Perhaps why I needed to add the %{HTTP:HOST} was because it's a multi virtual host setup.
来源:https://stackoverflow.com/questions/12422551/mod-rewrite-dont-match-on-rewritecond-target-url-not-found