htaccess 缩短 url

发布于 2024-10-03 06:08:55 字数 508 浏览 0 评论 0原文

我想缩短

www.site.com/product/info/laptop 至 www.site.com/laptop

我使用了

RewriteRule ^(.+)$ /product/info/$1

收到 500 内部服务器错误

但当我尝试时

RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()_=&-]+)$ /product/info/$1

,它有效,但我也想支持该期间,所以当我包含 .

RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()\._=&-]+)$ /product/info/$1

它给了我 500 Internal Server Error

你能解释一下发生了什么吗?

谢谢

i would like to shorten

www.site.com/product/info/laptop to www.site.com/laptop

I used

RewriteRule ^(.+)$ /product/info/$1

but i get 500 Internal Server Error

when i try,

RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()_=&-]+)$ /product/info/$1

it works but i want to support the period as well, so when I include .

RewriteRule ^([a-zA-Z0-9_\s\'~%,:!?()\._=&-]+)$ /product/info/$1

It gives me 500 Internal Server Error

Could you explain what is going on?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2024-10-10 06:08:55

请尝试以下操作:

RewriteCond %{REQUEST_URI} ^/product/info/(.*)$
RewriteRule ^(.*)?$        /%2                  [R=301,L]

这会将浏览器从 www.example.com/product/info/laptop 重定向到 www.example.com/laptop,并带有“永久移动”标头。

如果您希望较短的 URL 在内部指向较长的 URL,则必须避免循环重定向:

RewriteRule ^product/info/.*$ -                [L] # don't redirect again
RewriteRule ^(.*)$            /product/info/$1 [L] # redirect everything else

第一行将停止尝试将 www.example.com/product/info/laptop 重定向到 www.example.com/product /info/product/info/laptop 等。

编辑

根据您的评论,看起来您还试图将除 img、phpmyadmin 等之外的所有内容重定向到索引?无论如何 - 您现在必须重新排列一下,如下所示:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico|product/info/(.*))
RewriteRule ^(.*)$ - [L] # don't redirect these

RewriteRule ^(.*)$ /product/info/$1 # redirect everything else

我并不是 100% 同意第一次重写的“product/info/(.*)”部分。如果这不起作用,请尝试以下操作:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these

RewriteRule ^product/info/.*$ -                [L] # don't redirect again

RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else

编辑 2

根据您的评论的最终答案:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these

# pass controllers to index.php
RewriteCond %{REQUEST_URI} ^/(home|browse|calendar|star|member|enter|product)
RewriteRule ^(.*)$ /index.php?$1 [L]

# pass other things than controllers to /index.php?/product/info/$1
RewriteRule ^(.*)$ /index.php?/product/info/$1 [L] # redirect everything else

Try the following:

RewriteCond %{REQUEST_URI} ^/product/info/(.*)$
RewriteRule ^(.*)?$        /%2                  [R=301,L]

That will redirect the browser from www.example.com/product/info/laptop to www.example.com/laptop with a "Moved Permanently" header.

If you mean you wish the shorter URL to point to the longer URL internally, then you must avoid circular redirections:

RewriteRule ^product/info/.*$ -                [L] # don't redirect again
RewriteRule ^(.*)$            /product/info/$1 [L] # redirect everything else

The first line will stop trying to redirect www.example.com/product/info/laptop to www.example.com/product/info/product/info/laptop, and so-on.

Edit

Based on your comment, it looks like you're also trying to redirect everything except img, phpmyadmin, etc, to index?whatever - You have to rearrange it all a bit now, something like this:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico|product/info/(.*))
RewriteRule ^(.*)$ - [L] # don't redirect these

RewriteRule ^(.*)$ /product/info/$1 # redirect everything else

I'm not 100% on the "product/info/(.*)" part of the first rewrite. If that doesn't work, try this:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these

RewriteRule ^product/info/.*$ -                [L] # don't redirect again

RewriteRule ^(.*)$ /product/info/$1 [L] # redirect everything else

Edit 2

Final answer based on your comment:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(index\.php|img|phpmyadmin|images|user|wmd|FCKeditor|map|jscalendar|aurigma|xajax_js|css|js|include|floatbox|helper|styles|ajax|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ - [L] # don't redirect these

# pass controllers to index.php
RewriteCond %{REQUEST_URI} ^/(home|browse|calendar|star|member|enter|product)
RewriteRule ^(.*)$ /index.php?$1 [L]

# pass other things than controllers to /index.php?/product/info/$1
RewriteRule ^(.*)$ /index.php?/product/info/$1 [L] # redirect everything else
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文