开发者

Apache rewrite rule (url rewriting)

How can make a url rewriting for example, I want my to look like

www.m开发者_Python百科yurl.com/study-abroad/what/ever/

If I type

www.myurl.com/studyabroad/what/ever/

The idea is to have study-abroad instead of studyabroad


It can be done if you know in advance with how many dashes per URL you'd be confronted:

RewriteEngine On

# Replacing a single dash, e.g. example.com/study-abroad/what/ever
RewriteCond %{REQUEST_URI} ^([^-]*)-([^-]*)$
RewriteRule ^([^-]*)-([^-]*)$ $1$2 [L]

# Replacing two dashes, e.g. example.com/study-abroad/what-ever
RewriteCond %{REQUEST_URI} ^([^-]*)-([^-]*)-([^-]*)$
RewriteRule ^([^-]*)-([^-]*)-([^-]*)$ $1$2$3 [L]

... and so forth. May I add, though, that this is broken by design and you should reconsider your approach.

Regards


For such a simple example I would use mod_alias Redirects rather than mod_rewrite. Example:

Redirect permanent /studyabroad http://www.myurl.com/study-abroad

See http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirect for the full documentation.

You need to put this (or a similar) snippet into an Apache configuration file: if enabled (and if you have appropriate permissions), this might be .htaccess in your website's directory. Or, you could modify the master Apache config. See http://httpd.apache.org/docs/current/configuring.html for information about Apache configuration files.

(edited to invert the mapping: adding a dash instead of removing it)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜