简体   繁体   English

配置htaccess以进行url重写

[英]Configure htaccess for url rewriting

I need help for my url's rewriting. 我的网址重写需要帮助。 I want to organize my url's precisely by taking account of the current organization of my current tree: 我想通过考虑当前树的当前组织来组织我的URL:

-! /www

.htaccess,
index.php, ...

--! **/views**, /assets

pageA.php, pageB.php, pageC.php

---! **/pageA**, /pageB, /pageC, /errors (404.php, 500.php...)

pageA1.php, **pageA2.php**, pageA3.php

/!\\ I use specific names for each pages, the prefixes and numbers are just for examples and for a better comprehension about connexions between files and folders. /!\\我使用每个页面的特定名称,前缀和数字仅用于示例,以便更好地理解文件和文件夹之间的连接。 In reality I use pages names like: "contact-us.php", "our-product.php" ... 实际上我使用的页面名称如下:“contact-us.php”,“our-product.php”......

(1) When I going to pageA.php I would like this url path: www.mywebsite.fr/pageA (1)当我去pageA.php时,我想要这个网址: www.mywebsite.fr/pageA

(2) When I going to pageA2.php, I would like: www.mywebsite.fr/pageA/pageA2 (2)当我去pageA2.php时,我想: www.mywebsite.fr/pageA/pageA2

(3) I don't want extension files (.php, .html) (3)我不想要扩展文件(.php,.html)

Actually, I can't go into the path (2) cause I have a 404 page. 实际上,我不能进入路径(2)因为我有一个404页面。

I created folders with the same name as a specific php pages it's just for my own organization, but it can be a bad way (for SEO or anything else...) in fact, I don't know... 我创建了与特定php页面同名的文件夹,它只适用于我自己的组织,但它可能是一种不好的方式(对于SEO或其他任何东西......)其实我不知道...

My rewrite module (in .htaccess of www) is: 我的重写模块(在www的.htaccess中)是:

<IfModule mod_rewrite.c>

    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /

    # Retirer les extensions des pages et les rendres accessibles en lecture
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
    RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

    # check to see if the request is for a PHP file:
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^/?(.*)$ /$1.php [L]

    # Suppression d'un sous répertoire
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/views/$1.php -f 
    RewriteRule (.*) /views/$1.php [QSA,L]

</IfModule>

This is just off the top of my head, but it should work: 这只是我的头脑,但它应该工作:

RewriteRule ^page([A-Z]{1})([1-9]?).php$ http://www.mywebsite.fr/page$1$2
RewriteRule ^page([A-Z]{1})([1-9]{1})$ http://www.mywebsite.fr/page$1/page$1$2 [L,R=301]

The first Rewrite directive checks for a filename match starting with 第一个Rewrite指令检查以。开头的文件名匹配

page

and ending (prior to the filename extension, .php) with 和。结束(在文件扩展名之前,.php)

  • a single letter (from A to Z) ; 一个字母(从A到Z) ; and
  • (optionally) a single number (from 1 to 9) (可选) 单个数字(从1到9)

If it does, it captures both the letter (and the optional number) and rewrites the URI without the .php file extension at the end. 如果是,则捕获字母(和可选数字)并重写URI,最后不带.php文件扩展名。

For those URIs which do include the optional number, the second Rewrite directive captures both the letter and the number separately from each other. 对于那些包含可选数字的URI,第二个Rewrite指令会相互分别捕获字母和数字。

It then builds the redirect according to the correct folder structure, deploying both the captured letter and the captured number. 然后,它根据正确的文件夹结构构建重定向,部署捕获的字母和捕获的数字。

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

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