简体   繁体   English

使用.htaccess清理URL和可能的建议

[英]Clean URL using .htaccess and possible suggestions

I have the following url: 我有以下网址:

http://mywebsite.com/somepage.php?product=343&type=4

I want the user to be able to type: 我希望用户能够输入:

http://mywebsite.com/somepage?product=343&type=4

the role should handle pages in sub-directories and different file names (Not just Somepage or subdomain). 该角色应处理子目录中的页面和不同的文件名(不仅仅是Somepage或子域)。 For example: 例如:

http://mywebsite.com/subdomain/somepage.php?product=343&type=4

can be accessed by typing: 可以通过键入以下内容进行访问:

http://mywebsite.com/subdomain/somepage?product=343&type=4

I am also wondering if such links can be even cleaner? 我也想知道这样的链接是否可以更干净?

This will take any request that doesn't point to a file or directory and append ".php" if the .php file exists. 这将接受任何不指向文件或目录的请求,如果.php文件存在,则追加“ .php”。

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

This should solve the problem by only rewriting the request when the destination file exists. 这应该通过仅在目标文件存在时重写请求来解决问题。

I haven't tested this, but it might work. 我还没有测试过,但是可能有效。

RewriteEngine On
RewriteBase /

RewriteRule ^somepage(.*)$ somepage.php [QSA,L]

While the correct way is typically accepted as URL rewriting using .htaccess as @Nathan has shown, you can also optionally use MultiViews on a per-directory basis: 虽然使用@Nathan所示的.htaccess重写URL通常会接受正确的方法,但是您也可以选择基于每个目录使用MultiViews

<Directory /home/www/sitename/htdocs>
Options + MultiViews
</Directory>

Multiviews, on 404 from an unknown file extension, will begin searching for a match in the filename. 来自未知文件扩展名的404上的多视图将开始在文件名中搜索匹配项。 That is, if the URI is foo , it'll look for foo.* ; 也就是说,如果URI为foo ,它将寻找foo.* ; that said, it could potentially find foo.gif , foo.jpg , foo.html , etc., and will return the highest quality option. 也就是说,它可能会找到foo.giffoo.jpgfoo.html等,并会返回最高质量的选项。

If you're certain you could provide all your content on a unique-name basis (shouldn't be hard), then by all means use this. 如果您确定可以按唯一名称提供所有内容(不难),那么请务必使用此功能。 Then just change all your anchors to direct to somepage?product=343&type=4 instead of somepage.php?product=343&type=4 然后只需将所有锚点更改为直接指向somepage?product=343&type=4而不是somepage.php?product=343&type=4

edit : 编辑

Yes, such links can be made even cleaner. 是的,这样的链接可以变得更加整洁。 Using URL rewriting, you could rewrite your URLs to: 使用URL重写,您可以将URL重写为:

somepage/product/343/type/4 or somepage/343/4 somepage/product/343/type/4somepage/343/4

This is done by the appropriate (respective) regex: 这是通过适当的(分别)正则表达式完成的:

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)$ $1.php?$2=$3&$4=$5 [L,QSA]

or 要么

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)/(.*)/(.*)$ $1.php?product=$2&type=$3 [L,QSA]

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

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