linux - 用 .htaccess 重写长 url

标签 linux apache .htaccess ubuntu

我已经尝试在这里阅读花药问题,但它们对我不起作用。

今天我的.htaccess是这样的

    <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On

  # Ignore sitemap folder
  RewriteCond %{REQUEST_URI} !^/myapps/.*$

  # Get rid of index.php
  RewriteCond %{REQUEST_URI} /index\.php
  RewriteRule (.*) index.php?rewrite=2 [L,QSA]

  # Rewrite all directory-looking urls
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*) index.php?rewrite=1 [L,QSA]


  Redirect /acompanhantes h t t p://mysite.com/members?profile_type=4&displayname=&location=&within=50&lat=0&long=0&order=az&show=&1_4_9_alias_gender=&1_4_10_alias_birthdate%5Bmin%5D=&1_4_10_alias_birthdate%5Bmax%5D=&1_1_5_alias_gender=&1_1_6_alias_birthdate%5Bmin%5D=&$

  # Try to route missing files
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} public\/ [OR]
  RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
  RewriteRule . - [L]

  # If the file doesn't exist, rewrite to index
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]

</IfModule>

如您所见,我将长 url 重定向到 mysite.com/acompanhte

所以,我不想重定向。我想重写。有可能的?怎么办?

最佳答案

一些事情:

  1. 重写条件必须链接到规则,因此它会应用于紧随其后的重写规则。顶部的条件似乎没有执行任何操作,因为它已应用于 index.php 规则。

  2. Redirect 指令是 mod_alias 的一部分,其工作方式如下:Redirect A B,其中 A 是请求的 URL,< strong>B 是重定向TO 的地方。这意味着您必须在浏览器中键入 A URL,然后它告诉浏览器将位置更改B

  3. 此外,(来自 #2)这意味着为了使重定向执行任何有用的操作,您必须能够在浏览器中键入 B url 并让它返回您的内容重新期待(而不是类似错误的东西)。

  4. 由于Redirect是mod_alias,重写规则是mod_rewrite,两个模块会互相干扰,处理请求两次,所以这里只需要坚持使用mod_rewrite,使用R 标志。

也许你想要这样的东西?

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On

  RewriteCond %{THE_REQUEST} \ /+members?profile_type=4&displayname=&location=&within=50&lat=0&long=0&order=az&show=&1_4_9_alias_gender=&1_4_10_alias_birthdate%5Bmin%5D=&1_4_10_alias_birthdate%5Bmax%5D=&1_1_5_alias_gender=&1_1_6_alias_birthdate%5Bmin%5D=
  RewriteRule ^ /acompanhantes [L,R]

  # Ignore sitemap folder (passthrough these requests)
  RewriteRule ^myapps/ - [L]

  # Get rid of index.php
  RewriteCond %{REQUEST_URI} /index\.php
  RewriteRule (.*) index.php?rewrite=2 [L,QSA]

  # Rewrite all directory-looking urls
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*) index.php?rewrite=1 [L,QSA]

  # Try to route missing files
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} public\/ [OR]
  RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
  RewriteRule . - [L]

  # If the file doesn't exist, rewrite to index
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]

</IfModule>

关于linux - 用 .htaccess 重写长 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266210/

相关文章:

linux - bash 脚本 - 从文件夹中的选定文件打印 X 行

linux - Linux 中的以太网 + 串口

c++ - 如何在 C++ 中访问 Ubuntu key 环?

apache - Kubernetes拒绝使用apache/phpmyadmin连接

java - IOException : Could not initialize SSL context - Apache Commons Net - Android

apache - .htaccess 文件中哪个是正确/更好的规则?

regex - [[ $string = *[!0-9]* ]] 在 bash 中是什么意思?

php - 删除 apache 中的 Content-Type header

php - 在 URL 中显示 ID 号并将 ID 号中的数据库结果提取到文本字段中

php - .htaccess 的单一入口点显示 500 内部错误