简体   繁体   English

Mod重写(SEO友好URL)

[英]Mod Rewrite (SEO Friendly URL's)

You'd think I'd easily be able to find the answer to this on S/O, but I've tried everything and after a few hours of frustration I'm giving in and seeing what the real experts think. 您可能以为我可以轻松地在S / O上找到答案,但是我已经尝试了一切,经过数小时的挫折后,我屈服了,然后看看真正的专家的想法。

I'm "sure" this can be done with mod rewrite, but I'll defer to you. 我敢肯定,这可以通过mod rewrite来完成,但是我会顺便给你的。

Problem: I'm attempting to turn a URL like this... 问题:我正在尝试打开这样的网址...

http://domain.com/new-cars/state.php?stateCode=al

Into this at minimum... 至少要...

 http://domain.com/new-cars/al-new-cars

Though, ideally I'd get it to look like this (yes, I'm willing to rewrite some code to use the full state name as the $stateCode variable to make it easier!)... 不过,理想情况下,我希望它看起来像这样(是的,我愿意重写一些代码以将完整的状态名称用作$ stateCode变量,以使其变得更简单!)...

http://domain.com/new-cars/alabama-new-cars

Ultimately the plan is to be able to use URL's in links such as... 最终,该计划是要能够在诸如...的链接中使用URL。

http://domain.com/new-cars/alabama-new-cars

And have .htaccess take car of associating this SEO-friendly URL with the dynamic version and displaying the page properly. 并让.htaccess负责将此SEO友好URL与动态版本相关联并正确显示页面。

Either way, I haven't been able to figure out how to do this like I need. 无论哪种方式,我都无法根据自己的需要弄清楚该如何做。

Here's what I've tried. 这是我尝试过的。

  Options +FollowSymlinks RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^new-cars/([^-]*)-new-cars/$ /new-cars/state.php?stateCode=$1 [L,QSA,NC] 

And different variations that I've created using 2 different mod rewrite generators and various answers to other people's questions. 我使用2个不同的mod重写生成器以及对其他人的问题的各种答案创建的各种变体。

Absolutely nothing is working. 绝对没有任何作用。

I expect when I go to 我希望当我去

http://domain.com/new-cars/state.php?stateCode=AL

That it rewrites the URL to 它将URL重写为

http://domain.com/new-cars/AL-new-cars

...but it does not. ...但事实并非如此。 Instead, it stays exactly the same dynamic URL I typed in. If I go to the "desired" rewrite URL I get a 404 error saying the page doesn't exist. 取而代之的是,它保持与我键入的动态URL完全相同。如果我转到“所需”重写URL,则会收到404错误,表明该页面不存在。

What am I doing wrong? 我究竟做错了什么?

I thought maybe my .htaccess privileges weren't set right, but I can do a 301 redirect through .htaccess quite easily, so that's not it. 我以为可能我的.htaccess权限设置不正确,但是我可以很容易地通过.htaccess进行301重定向,事实并非如此。

Maybe someone here can help. 也许有人可以帮忙。 I've tried to so many permutations, even settling for the most basic rewrite just to see if I could get it to work - but nothing. 我已经尝试了很多排列,甚至为了最基本的重写而安定下来,只是为了看看我是否可以使它工作-但是什么也没有。

Any help is appreciated! 任何帮助表示赞赏!

You can use: 您可以使用:

Options +FollowSymlinks

RewriteEngine on
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+new-cars/state\.php\?stateCode=([^\s&]+) [NC]
RewriteRule ^ /new-cars/%1-new-cars? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^new-cars/([^-]*)-new-cars/?$ /new-cars/state.php?stateCode=$1 [L,QSA,NC]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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