首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除扩展并使用tralling斜杠添加get值

删除扩展并使用tralling斜杠添加get值
EN

Stack Overflow用户
提问于 2015-12-26 15:39:08
回答 1查看 38关注 0票数 1

要做到这一点:

原始URL -> localhost/viewprofile.php

ReWrite URL -> localhost/viewprofile/

我用了这个密码。

代码语言:javascript
复制
# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
 </IfModule>

为了做这件事我用

原始URL -> localhost/viewprofile.php?user_id = 1

ReWrite URL -> localhost/1/

代码语言:javascript
复制
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ viewprofile.php?user_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewprofile.php?user_id=$1

但不幸的是我想这么做

原始URL -> localhost/viewprofile.php?user_id = 1

ReWrite URL -> localhost/viewprofile/1/

和这个

原始URL -> localhost/viewprofile.php?username=sean

ReWrite URL -> localhost/viewprofile/sean/

我有这两个Original URLs,我想像上面给出的那样将它们分别重写为各自的ReWrite URL

有什么解决办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-26 15:47:58

您可以在数字(仅)上匹配第一个数字和另一个数字(例如)的字母数字。

代码语言:javascript
复制
<IfModule mod_rewrite.c>
  Options +FollowSymLinks -MultiViews
  RewriteEngine On
  RewriteBase /

  # Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.+)$ $1/ [R=301,L]

  # Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
  RewriteRule ^(.+)/$ $1.php [L]

  RewriteRule ^viewprofile/([0-9]+)/?$ viewprofile.php?user_id=$1 [L]
  RewriteRule ^viewprofile/([A-Za-z0-9]+)/?$ viewprofile.php?username=$1 [L]
</IfModule>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34472765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档