简体   繁体   English

htaccess将段塞重写为seo url

[英]htaccess rewrite slug to seo url

I have some URLs like this: 我有一些这样的网址:

www.mydomain/works/show.php?slug=title-of-the-work www.mydomain /工程/ show.php?塞=冠军的最工作

I'll like to have htaccess convert to seo URLs in this way: 我希望htaccess以这种方式转换为seo URL:

www.mydomain/works/title-of-the-work www.mydomain /工程/冠军的最工作

In my database I have created a field called slug for every work entry. 在数据库中,我为每个工作条目创建了一个名为slug的字段。 The first URL works fine. 第一个URL工作正常。

This is how my .htaccess currently looks (it is placed in my root directory): 这是我的.htaccess当前的外观(位于我的根目录中):

RewriteEngine On
RewriteRule /works/(.*)$ /works/show.php?slug=$1

I've read and tried a lot of similar examples during the last 24hours to no avail. 在过去的24小时内,我已经阅读并尝试了许多类似的示例,但均无济于事。 I'm sure my server allows rewriting, because I can for example rewrite non-www to www URLs. 我确定我的服务器允许重写,因为例如可以将非www重写为www URL。 Hope someone can help. 希望有人能帮忙。 Thanks 谢谢

Use: 采用:

RewriteEngine On
RewriteRule ^works/(.*)$ works/show.php?slug=$1 [L]

You can also put your .htaccess file into works folder with this code: 你也可以把你的.htaccess文件到工程与此代码文件夹:

RewriteEngine On
RewriteRule ^(.*)$ show.php?slug=$1 [L]

Finally this is the htaccess I'm using (inside works folder) : 最后,这是我正在使用的htaccess(在works文件夹中):

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)/?$ show.php?slug=$1 [L]

Thanks userNOID for putting me on the right track. 感谢userNOID将我带入正确的轨道。 There was indeed a redirection loop between htaccess and my php code, which was solved by adding the two conditions above to tell the server to ignore the redirection if the file phisically exists (true in 4 cases: /works, /works/web-design, /works/graphic-design and works/apps) 确实在htaccess和我的php代码之间存在重定向循环,通过添加上述两个条件来告诉服务器如果文件物理存在,则忽略服务器重定向(在4种情况下为true:/ works,/ works / web-design),此问题得以解决,/ works / graphic-design和works / apps)

I also changed the regular expression a bit to be able to have the URLs work with and without trailing slash. 我还对正则表达式做了一些更改,以使URL可以使用和不使用斜杠。

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

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