简体   繁体   English

使用.htaccess(而非wordpress)删除自定义网址上的结尾斜杠

[英]Removing trailing slash on custom urls with .htaccess (not wordpress)

I'm new to learning HTACCESS and I'm trying my best to work on a websites URLs to make them more "seo friendly" and load properly. 我是HTACCESS的新手,我正在尽最大努力在网站URL上工作,以使它们更加“ SEO友好”并正确加载。 I've tried searching all over for what I need to point me in the right direction, but everything seems to be for "wordpress" and this is a custom built site. 我尝试到处搜索以寻找正确的方向,但是一切似乎都是针对“ wordpress”的,这是一个自定义网站。

Here is my issue. 这是我的问题。 This site had a previous developer, the thing was a mess, and I've spent countless hours cleaning up the site, and he built the sites to show urls like this. 这个站点有一个以前的开发人员,真是一团糟,我花了无数个小时清理这个站点,他建立了这些站点来显示这样的URL。 "www.sitename.com/about-us.php" “ www.sitename.com/about-us.php”

I'm familiar enough with htaccess to know how to remove the trailing extension for .php, even showing trailing variables properly that doesn't require the trailing slash at the end like: "sitename.com/about-us?contact=true" 我对htaccess足够熟悉,知道如何删除.php的尾随扩展名,甚至正确显示了尾部不需要尾部斜杠的尾部变量,例如:“ sitename.com/about-us?contact=true”

I was able to get the blog view page to load with a url like this: "sitename.com/posts/post-name-here/". 我能够通过以下网址加载博客视图页面:“ sitename.com/posts/post-name-here/”。 This one though does require the trailing slash. 虽然这一点确实需要在末尾加斜杠。

What I'm trying to accomplish is making the url string to work properly with trailing variables for a few pages like, register, apply, etc. So those urls would be something like this. 我要完成的工作是使url字符串与某些页面(如注册,应用等)的尾随变量一起正常工作。因此,这些url就是这样。 "sitename.com/auth/apply" or "sitename.com/auth/register" “ sitename.com/auth/apply”或“ sitename.com/auth/register”

My issue is that the urls have to have a trailing slash at the end or it doesn't load. 我的问题是,网址末尾必须带有斜杠,否则无法加载。

Plus adding a trailing variables only works when formatted like this: "sitename.com/auth/apply/?application=submitted" 另外,添加尾随变量仅适用于以下格式:“ sitename.com/auth/apply/?application=submitted”

I'd prefer it look more like this. 我希望它看起来更像这样。 "sitename.com/auth/apply?application=submitted", without the trailing slash. “ sitename.com/auth/apply?application=submitted”,不带斜杠。 But I can't find how to accomplish this. 但我找不到如何实现这一目标。

Here is my .htaccess snippet. 这是我的.htaccess片段。

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^posts\/(.*)/$ /view-post.php?post=$1 [QSA,NC,L]
RewriteRule ^auth\/(.*)/$ /auth-action.php?action=$1 [QSA,NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Can someone help me, or point me in the right direction? 有人可以帮助我,或为我指明正确的方向吗?

Try using the following in your .htaccess . 尝试在.htaccess使用以下内容。 This will remove the trailing slash from your URLs. 这将从您的网址中删除斜杠。

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=302,L]

Make sure you clear your cache before testing this. 在测试之前,请确保清除缓存。 You'll notice that I've set R=302 instead of 301 . 您会注意到,我将R=302设置为301 This makes it a temporary redirect for testing purposes. 这使其成为用于测试目的的临时重定向。 If you're happy with the redirect then change it to 301 to make it permanent. 如果您对重定向感到满意,请将其更改为301以使其永久不变。

This should be what you're looking for. 这应该是您要寻找的。

#Options +FollowSymLinks
Options -Indexes
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*) index.php [QSA,NC,L]

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

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