Tips Rewrite Url với page php tự tạo trong Wordpress

### Viết lại URL với trang PHP tự chế trong WordPress

** #wordpress #Php #Url #REWRITE #Hướng dẫn **

WordPress là một hệ thống quản lý nội dung tuyệt vời, nhưng nó không hoàn hảo.Một trong những điều có thể là một nỗi đau là cách tạo ra URL.Theo mặc định, WordPress sử dụng định dạng khá xấu xí cho các URL, như `https: // example.com/blog/post-name/`.Đây có thể là một vấn đề nếu bạn muốn có các URL đẹp, sạch cho các bài đăng trên blog của bạn.

May mắn thay, thật dễ dàng để viết lại các URL trong WordPress bằng trang PHP tự chế.Đây là cách làm điều đó:

1. ** Tạo một tệp PHP mới. ** Bạn có thể đặt tên cho nó bất cứ thứ gì bạn muốn, nhưng tôi sẽ gọi cho tôi `rewrite.php`.
2. ** Thêm mã sau vào tệp: **

`` `PHP
<? PHP
// Nhận URL hiện tại.
$ current_url = $ _server ['request_uri'];

// Xóa phần 'wp-admin' của URL.
$ current_url = preg_replace ('/wp-admin \/?/', '', $ current_url);

// Viết lại url.
$ new_url = str_replace ('-', '_', sanitize_title ($ current_url));

// Chuyển hướng đến URL mới.
tiêu đề ('vị trí:'. $ new_url);
lối ra();
`` `

3. ** Tải tệp lên trang web WordPress của bạn. ** Bạn có thể thực hiện việc này bằng FTP hoặc trình quản lý tệp của bạn.
4. ** Kích hoạt các quy tắc viết lại. ** Để kích hoạt các quy tắc viết lại, bạn cần thêm mã sau vào tệp `.htaccess` của bạn:

`` `
Viết lại trên
Viết lại ^viết lại.php $ - [l]
Viết lại %{request_filename}! -F
Viết lại %{request_filename}! -D
Viết lại ^(.*) $ Rewrite.php? Url = $ 1 [l]
`` `

5. ** Kiểm tra các quy tắc viết lại của bạn. ** Khi bạn đã thêm mã vào tệp `.htaccess` của bạn, hãy thử truy cập một bài đăng trên blog của bạn.URL bây giờ sẽ được viết lại bằng định dạng mà bạn đã chỉ định trong tệp PHP của mình.

Đó là nó!Bây giờ bạn đã viết lại thành công các URL trong WordPress bằng trang PHP tự chế.

### 5 hashtags

* #wordpress
* #Php
* #Url
* #Rewriting
* #tutorial
=======================================
### Rewrite URL with homemade PHP page in WordPress

**#WordPress #Php #Url #Rewriting #tutorial**

WordPress is a great content management system, but it's not perfect. One of the things that can be a pain is the way that URLs are generated. By default, WordPress uses a pretty ugly format for URLs, like `https://example.com/blog/post-name/`. This can be a problem if you want to have nice, clean URLs for your blog posts.

Fortunately, it's easy to rewrite URLs in WordPress using a homemade PHP page. Here's how to do it:

1. **Create a new PHP file.** You can name it anything you want, but I'll call mine `rewrite.php`.
2. **Add the following code to the file:**

```php
<?php
// Get the current URL.
$current_url = $_SERVER['REQUEST_URI'];

// Remove the 'wp-admin' part of the URL.
$current_url = preg_replace('/wp-admin\/?/', '', $current_url);

// Rewrite the URL.
$new_url = str_replace('-', '_', sanitize_title($current_url));

// Redirect to the new URL.
header('Location: ' . $new_url);
exit();
```

3. **Upload the file to your WordPress site.** You can do this using FTP or your file manager.
4. **Activate the rewrite rules.** To activate the rewrite rules, you need to add the following code to your `.htaccess` file:

```
RewriteEngine On
RewriteRule ^rewrite.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ rewrite.php?url=$1 [L]
```

5. **Test your rewrite rules.** Once you've added the code to your `.htaccess` file, try visiting a post on your blog. The URL should now be rewritten using the format that you specified in your PHP file.

That's it! You've now successfully rewritten URLs in WordPress using a homemade PHP page.

### 5 Hashtags

* #wordpress
* #Php
* #Url
* #Rewriting
* #tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top