Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache - rewrite url using mod rewrite without redirection

this url is being rewritten

http://domain.com/embed/slideshow-image-list.js

to this

http://domain.com/code/embed/slideshow-image-list

current htaccess code

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteRule ^embed/([^.]+).js$ http://domain.com/code/embed/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

its currently rewritten correctly, but if you visit the .js url it redirects to the full url.

im looking just to map it without redirection

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't supply the domain in the rule, just the path. With the full domain it will be redirected.

RewriteRule ^embed/([^.]+).js$ /code/embed/$1 [L]

Alternately, if the resulting path is not a real file and should be handled by index.php, stip off the [L] so that the next rule will be executed:

RewriteRule ^embed/([^.]+).js$ /code/embed/$1

Note that the mod_rewrite guide says the following, which would seem to indicate that the full URL path is okay, but I believe it compares against ServerName, not necessarily ServerAlias (I'm not 100% sure about that, but I've always used paths rather than full URLs in doing non-redirect rewrites):

Absolute URL

If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...