简体   繁体   中英

Rewrite long url with .htaccess

I have already tried to read anthers questions here, but they not working for me.

Today, my .htaccess is like this

    <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>

As you can see, I redirect the long url to mysite.com/acompanhte

So, I´d like not to redirect. I´d like to rewrite. It is possible? How?

A few things:

  1. A rewrite condition must be linked to a rule, so it is applied to the immediately following rewrite rule. You have a condition at the top that doesn't appear to be doing anything since it's applied to the index.php rule.

  2. The Redirect directive is part of mod_alias and works like this: Redirect AB , where A is the requested URL and B is where it redirects TO . That means you must type the A URL into your browser and then it tells the browser to change the location to B .

  3. Additionally, (from #2) that means in order for that redirect to do anything useful, you must be able to type B url into your browser and have it return what you're expecting (and not something like an error).

  4. Since Redirect is mod_alias and the rewrite rules are mod_rewrite, the two modules will interfere with each other and process requests twice, so you need to just stick with mod_rewrite here and use the R flag.

Maybe you want something like this?

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM