简体   繁体   English

.htaccess PHP 参数友好 URL

[英].htaccess PHP Parameter Friendly URL

I would like to make the URLs of my Store URL-friendly.我想让我的商店 URL 的 URL 友好。

Current URL Structure当前 URL 结构

https://my-domain.com/store/store.php?page=packages&id=1

Desired URL Structure所需的 URL 结构

https://my-domain.com/store/packages/1

And also for direct access to the PHP files such as:并且还可以直接访问 PHP 文件,例如:

https://my-domain.com/store/profile.php to https://my-domain.com/store/profile https://my-domain.com/store/profile.phphttps://my-domain.com/store/profile

How would I need to go after this?在此之后我需要 go 吗? I really appreciate any help you can provide.我真的很感激你能提供的任何帮助。

Also might be note worthy that in the base directory a WordPress site is running with its own .htaccess file.还可能值得注意的是,在基本目录中,一个 WordPress 站点正在运行它自己的 .htaccess 文件。

I already tried it with this我已经用这个试过了

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/store/page/(.*)/id/(.*) /store/store.php?page=$1&id=$2
RewriteRule ^store/store/page/(.*)/id/(.*)/ /store/store.php?page=$1&id=$2

But that didn't work但这没有用

You can use this for the first part:您可以在第一部分使用它:

RewriteRule ^store/((?.store)[^/]+)/([^/]+)$ /store/store?php?page=$1&id=$2 [L]

This code will work.此代码将起作用。

RewriteEngine will remove.php from all PHP Files RewriteRule will rewrite url like page/id RewriteEngine将从所有PHP文件中删除.php RewriteRule将重写url like page/id

For Removing.php extension对于删除.php 扩展名

RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

For page/id对于页面/id


<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)? store.php?page=$1&id=$2 [L] 
</IfModule>

Although nothing is wrong with anyone else's answers, the more modern way to do this (including WordPress, Symfony and Laravel) is to send non-existent URLs to a single router script.尽管其他人的答案都没有问题,但更现代的方法(包括 WordPress、Symfony 和 Laravel)是将不存在的 URL 发送到单个路由器脚本。 By doing this, you only have to mess with an.htaccess file once to set things up, and never touch it again if you add more "sub-folders", you can do all of that in just PHP. This is also more portable which means you can bring it to other server platforms such as Nginx with little changes, and don't need to deal with RegEx.通过这样做,你只需要弄乱 .htaccess 文件一次就可以设置,如果你添加更多的“子文件夹”就再也不用碰它了,你可以在 PHP 中完成所有这些。这也更便携这意味着您可以将它带到其他服务器平台,例如 Nginx,只需稍作改动,并且不需要处理 RegEx。

The.htaccess is fairly straightforward. .htaccess 相当简单。 Route all requests that start with /store/ and don't exist as a file (such as images, JS and CSS) or directory to a single new file called router.php in your /store/ folder.将所有以/store/开头且不作为文件(例如图像、JS 和 CSS)或目录存在的请求路由到/store/文件夹中名为router.php的单个新文件。 This is an internal redirect, which means it isn't a 301 or 302.这是一个内部重定向,这意味着它不是 301 或 302。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/ /store/router.php [L]

Then in your new router.php file you can parse $_SERVER['REQUEST_URI'] to determine the URL that was actually requested, and you can even rebuild the global $_GET variable:然后在你的新router.php文件中你可以解析$_SERVER['REQUEST_URI']来确定实际请求的 URL,你甚至可以重建全局$_GET变量:

// Parse the originally requested URL into parts
$requestUrlParts = parse_url($_SERVER['REQUEST_URI']);

// Parse the query string into parts, erase the old global _GET array
parse_str($requestUrlParts['query'], $_GET);

// Handle 
switch($requestUrlParts['path']){
    case '/store/store.php';
        include '/store/store.php';
        exit;
        
    // Custom 404 logic here
    default:
        http_response_code(404);
        echo 'The page you are looking for cannot be found';
        exit;
}

I'd also recommend putting the.htaccess rule into the site root's.htaccess folder, above WordPress's.我还建议将 .htaccess 规则放入站点根目录的 .htaccess 文件夹中,在 WordPress 的上方。 There's nothing wrong with creating multiple files, this just keeps things in a central place and makes it easier (IMHO) to debug.创建多个文件没有错,这只是将事情放在一个中心位置并使其更容易(恕我直言)调试。

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

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