Apache 2.4 URL rewriting: /server/?Server=ServerName to /server/ServerName
P粉038161873
P粉038161873 2024-01-16 19:21:09
0
1
434

I am trying to simply rewrite the URL from /server/?Server=ServerName to /server/ServerName so that the ServerName parameter is passed to PHP .

I currently have this in my .htaccess file:

RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} Server=([^&]+)
RewriteRule "^server/$" "/server/%1/?"

A 404 error occurred when trying to view /server/ServerName or /server/?Server=ServerName.

This is my rewrite log:

init rewrite engine with requested uri /server/
pass through /server/
strip per-dir prefix: C:/wamp64/www/server/ -> server/
applying pattern '^server/$' to uri 'server/'
RewriteCond: input='Server=ServerName' pattern='Server=([^&]+)' => matched
rewrite 'server/' -> '/server/ServerName/?'
split uri=/server/ServerName/? -> uri=/server/ServerName/, args=<none>
trying to replace prefix C:/wamp64/www/ with /
trying to replace context docroot C:/wamp64/www with context prefix 
internal redirect with /server/ServerName/ [INTERNAL REDIRECT]

At this point, I need a little guidance because I'm frustrated and I feel like I'm missing a simple thing.

I have tried making adjustments:

  • mod_rewrite query string returns 404
  • Difference between $1 and %1 in .htaccess
  • Why do I get a 404 error when using .htaccess RewriteRule?
  • Using the example at https://httpd.apache.org/docs/2.4/rewrite/remapping.html
  • Quoting markup from https://cheatography.com/davechild/cheat-sheets/mod-rewrite/

I've also been looking into rewriting the code, trying to see if it's actually rewriting.

Using the code below I successfully rewrote the directory /test2/ to /server/?Server=ServerName and the page loaded fine, so I know the module is active and running.

RewriteRule ^test2/$ /server/?Server=ServerName [PT,L,R=301]

An attempt was made to make this change without success. The URL will not be rewritten, but the page will load. Apache 2.4 .htaccess friendly URL rewriting

RewriteRule ^server/([^/\.]+)/?$ /server/?Server=? [L]

P粉038161873
P粉038161873

reply all(1)
P粉182218860

You seem to be going in the wrong direction. You should link to /server/ServerName in the HTML source, so internally the request is rewritten from /server/ServerName to /em> /server/ index.php?Server=ServerName (note adding index.php as mentioned in the comments). /server/index.php?Server=ServerName is the path to the underlying (hidden) file that actually handles the request.

Although you seem to have mentioned it in the last line...

URL must be rewritten or the page will not load. There may be some confusion about what exactly a "rewrite" is. This does not change the URL. You don't use .htaccess to change the URL. You must manually change the URL in the HTML source - the URL you want to link to.

If you wish, you can (optionally) implement an external "redirect" later - this does change the URL. But this is only used to redirect search engines and third parties that may link to or index the old URL. So, this is for SEO, not to make your app "work".

So, in summary, in this order:

  1. Change the URL in the HTML source code to link to a URL of the form /server/ServerName.
  2. (Optional) If you are changing the existing URL structure, redirect any direct requests to /server/?Server=ServerName (or /server/index.php?Server =ServerName) to /server/ServerName (canonical URL).
  3. Internally rewrites the request from /server/ServerName (the canonical URL/the URL you want to link to) to /server/index.php?Server=ServerName (process the requested underlying file).

In the root .htaccess file:

RewriteEngine On

# (OPTIONAL) For SEO, if changing an existing URL structure...
# Redirect "/server/?Server=ServerName" to "/server/ServerName"
RewriteCond %{QUERY_STRING} ^Server=([^&./]+)(&|$) [NC]
RewriteRule ^server/(index\.php)?$ /server/%1 [NE,R=301,L]

# Rewrite request from "/server/ServerName" to "/server/index.php?Server=ServerName"
RewriteRule ^server/([^/.]+)$ server/index.php?Server= [END]

This assumes you are using Apache 2.4 to be able to use the END flag in the last rule. This avoids having to have an extra condition in the first rule that checks for direct requests instead of "rewritten" requests (to avoid redirect loops).

In the first rule ("Redirect"), the index.php part is optional. %1 The backreference contains the value of the Server URL parameter captured in the preceding condition. Server URL parameters must be non-empty and not contain dots or slashes (according to the regular expression you gave in the previous rule), otherwise they will not be redirected or rewritten. Note that literal dots in regular expression character classes do not require backslash escaping.

Please note that I am assuming (based on your example) that the requested URL does not end with a trailing slash (i.e. /server/ServerName rather than /server/ ServerName/), so I removed the optional /?last rule at the end of RewriteRule pattern. Although your log excerpt seems to suggest otherwise? If you really need to allow trailing slash and non-trailing slash URLs, then this should be implemented as a separate "redirect" in order to normalize the URL, rather than as part of a "rewrite" which would otherwise promote duplicate content (slashes and URLs without slashes are different URLs).

Depending on what you expect in the Server URL parameter value, the NE (noescape) flag may or may not be required. QUERY_STRING Server variables are URL encoded.

You do not need to use the PT (passthrough) flag in .htaccess as this is the default behavior in this context. (The PT flag is only relevant when using mod_rewrite in the context of a server or virtualhost.)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!