简体   繁体   English

将URL重写为所有小写字母,然后使用.htaccess文件将某些URL重定向到主页

[英]Rewrite URL to all lower case letters and redirect some URL to home page using .htaccess file

My problem is I want to write my own rule for .htaccess file. 我的问题是我想为.htaccess文件编写自己的规则。 I have searched the internet, they didn't told me how to write my own specific rule. 我搜索了互联网,他们没有告诉我如何编写自己的特定规则。 All I know is that it uses regular expressions. 我所知道的是它使用正则表达式。 I have little knowledge of regex. 我对正则表达式一无所知。

What I am looking for is I have the following links 我正在寻找的是以下链接

http://example.com/work/project-1 
http://example.com/work/project-2

when user enter like this 当用户这样输入时

http://example.com/work/PrOJect-2

I want them to redirect to (converting all caps to small) 我希望他们重定向到(将所有大写字母转换为小写)

http://example.com/work/project-2 

But when user enter like this 但是当用户这样输入

http://example.com/work/ 

or 要么

http://example.com/work  

I want to redirect to home page. 我想重定向到主页。

For doing so I have htaccess file which have some rules like 为此,我有htaccess文件,其中包含一些规则,例如

RewriteEngine On
RewriteRule ^work(.*)/([^/]*) work-single.php$1?slug=$2 [QSA]

Please help me to write these rules. 请帮助我编写这些规则。

What you are trying to achieve is called URL rewriting. 您试图实现的目标称为URL重写。

Here's an example of a working .htaccess file: 这是一个有效的.htaccess文件的示例:

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_params=$1 [L,QSA]

The result of using the above rules set is that the full URL from the request is being passed on the server-side to index.php, where the string can be fetched using: 使用上述规则集的结果是,来自请求的完整URL在服务器端传递到index.php,可使用以下命令在其中获取字符串:

$urlString = $_GET['url_params'];

and further processed, for an example slicing the string into an array of tokens: 并进行进一步处理,例如将字符串切成令牌数组:

$urlTokens = explode('/', $urlString);

foreach($urlTokens as $token){

    // do something with each token

}

and finally analyse the tokens (a task that most people these days dont actually manually do becuase of the use of frameworks that take care of this automatically. eg Zend framework), for each valid "token" you take the appropriate action (in modern frameworks this is called "Routing"). 最后分析令牌(如今,大多数人实际上并不手动执行此任务,原因是使用了自动处理此问题的框架。例如Zend框架),对于每个有效的“令牌”,您都应采取适当的措施(在现代框架中)这称为“路由”)。

For a complete tutorial on how to setup url rewriting, you can check: https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049 有关如何设置URL重写的完整教程,可以查看: https : //code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049

It can be pretty straight forward, or completely complicated - depends on your needs. 它可以很简单,也可以完全复杂-取决于您的需求。

Bear in mind that this is only an example and might not work for your exact situation, you might want to customise it to fit your needs - or at least learn from it and solve your problem. 请记住,这只是一个示例,可能无法满足您的实际情况,您可能想要对其进行自定义以满足您的需要-或至少从中学习并解决问题。

I would recommend that you read and learn more about the subject, it will make it easier for you to use the technique. 我建议您阅读并了解有关该主题的更多信息,这将使您更轻松地使用该技术。

Hope it helps a bit 希望能有所帮助

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

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