Apache mod Rewrite:如何重写这些URL以生成干净的URL [英] Apache mod Rewrite: how to rewrite these URLs to make clean URL

查看:82
本文介绍了Apache mod Rewrite:如何重写这些URL以生成干净的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从最近两天开始重写。

I'm trying do following rewrite from last two days.


  1. 强制使用www.example.com-完成

  2. 隐藏PHP扩展-完成但不确定这是一个好方法

  3. 自定义404错误页面-完成

  1. Force to use www.example.com - done
  2. Hide PHP extension - done but not sure that it's a good way
  3. 404 error to custom page - done

现在我的主要问题是

4。将所有页面托管在root(/)上

我有以下文件:

news.php
search.php
store.php
allstores.php

客户请求是这样的:

Type 1: http://www.example.com/news

Type 2: http://www.example.com/search?q=term

Type 3: http://www.example.com/allstores/H

Type 4: http://www.example.com/myname

我想这样处理这些客户请求:

I want to handle these client request like this:

Type 1: http://www.example.com/news  ---rewrite to (add extension)-->news.php

Type 2: http://www.example.com/search?q=term  ---->No change, Let it same

Type 3: http://www.example.com/allstores/H --rewrite to --->http://www.example.com/allstores.php?ln=H

Type 4: http://www.example.com/myname ---rewrite to------> http://www.example.com/store?st=myname

我尝试过以下重写:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteRule ^(.*) store.php?st=$1 [L]
RewriteRule ^allstores/(.*)$ allstores.php?ln=$1 [L]

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

ErrorDocument 404 /404.php

任何帮助都是非常感谢的,谢谢

Any help is highly appreciable, Thank you

推荐答案

您可以尝试:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^allstores/([^/]+)/?$ /allstores.php?ln=$1 [L,QSA,NC]

# To internally forward /foo to /foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

RewriteCond %{QUERY_STRING} !^q=.+ [NC]
RewriteRule ^([^/]+)/?$ /store.php?st=$1 [L,QSA]

这篇关于Apache mod Rewrite:如何重写这些URL以生成干净的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆