大网数据广告活动
网站运营与故障问答

How to replace '?' and `=` with slashes in a URL using .htaccess?

· 52CE 我爱测

How to replace ? and = with / in the URL? I think it may be in .htaccess . Example: example.com/public/Micheal-Smith would look like example.com?public=Micheal-Smith if I didn't replace the ? and = with / .

问题原文

原帖链接 · Wawi Sobota · CC BY-SA 4.0

How to replace ? and = with / in the URL? I think it may be in .htaccess.

Example:

example.com/public/Micheal-Smith would look like example.com?public=Micheal-Smith if I didn't replace the ? and = with /.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

I'm just trying to change URL in my application.

In that case, you are really asking the wrong question, since in order to change the URL that your application uses you don't "replace ? and = with /" in .htaccess. (You might do this later in order to preserve SEO, but that is really secondary to getting this working.)

In order to change the URL in your application, you must first actually change the URL you are using in your application. For example, if you were previously linking to a URL of the form example.com?public=Micheal-Smith in your HTML then you must physically change that link point to example.com/public/Micheal-Smith (the "new" user-friendly URL) instead. There really is no shortcut here. You must change the actual link in your HTML source.

You then use mod_rewrite (Apache's URL-rewriting module) in .htaccess to internally rewrite a request for the "new" user-friendly URL back into the "old" URL that your application understands. ie. /public/Micheal-Smith to ?public=Micheal-Smith. In other words, you are converting the slashes back into ? and = (effectively the opposite of what you were asking in the question).

Another side issue with how you've asked the question is that example.com?public=Micheal-Smith is not strictly a valid end-point. It requires further rewriting (perhaps by mod_dir) to actually get the request to a script that handles the response. If you are using PHP and the request should be handled by index.php in the document root, then this should really be example.com/index.php?public=Micheal-Smith (note there is always a slash at the start of the URL-path, even though you might not see this in the browser).

From your example, I'm assuming public (the URL parameter name) and Micheal-Smith (the URL parameter value) are entirely variable, so in a more generalised sense, you are changing the URL from /index.php?<param-name>=<param-value> to /<param-name>/<param-value>. I'll also assume <param-name> and <param-value> are restricted to the characters 0-9, a-z, A-Z, _ (underscore) and - (hyphen).

So, in summary, the steps to implement this are:

  1. Actually change the URL in your HTML source to /<param-name>/<param-value>.

  2. Implement an internal rewrite in .htaccess to rewrite a request for /<param-name>/<param-value> back to /index.php?<param-name>=<param-value>. For example:

    RewriteEngine On
    
    # Rewrite "/<param-name>/<param-value>" to "/index.php?<param-name>=<param-value>"
    RewriteRule ^([\w-]+)/([\w-]+)$ index.php?$1=$2 [QSA,L]
    

    The first argument ^([\w-]+)/([\w-]+)$ is a regular expression that matches against the requested URL /<param-name>/<param-value>. The parenthesised subpatterns (ie. ([\w-]+)) are "capturing groups" that capture the parts of the URL before and after the slash (the <param-name> and <param-value>). These are saved in the $1 and $2 backreferences and used in the RewriteRule substitution string (2nd argument) to form the original query string parameter. The QSA flag appends any query string that might be present on the requested URL to the end of the susbstituion string. If you are not using any additional URL parameters then you can remove this flag.

That is sufficient to implement the "new" user-friendly URL.

Optional 3rd step... However, if you are changing an existing URL structure that has already been indexed by search engines and linked to by third parties then you can choose to implement an external redirect from /?<param-name>=<param-value> to the "new" user-friendly URL, ie. /<param-name>/<param-value> (effectively the opposite of the above rewrite). This is a necessary step in order to preserve SEO.

  1. Implement an external redirect in .htaccess to redirect a request for /?<param-name>=<param-value> to /<param-name>/<param-value> (the "new" canonical URL). This needs to go before the above rewrite, immediately after the RewriteEngine directive:

    # Redirect "/?<param-name>=<param-value>" to "/<param-name>/<param-value>"
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{QUERY_STRING} ^([\w-]+)=([\w-]+)$
    RewriteRule ^(index\.php)?$ /$1/$2 [R=301,L]
    

    The index.php part of the URL is entirely optional, so it matches both /?<param-name>=<param-value> and /index.php?<param-name>=<param-value>.

    The first condition that checks against the REDIRECT_STATUS environment variable is necessary in order to redirect direct requests only and not rewritten requests by the later rewrite - which would otherwise cause a redirect loop.

    However, always test first with a 302 (temporary) redirect in order to avoid potential caching issues and only change to a 301 (permanent) redirect once you have tested that it works as intended.

Reference:

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-05-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。