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:
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]
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 addingindex.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:
/server/ServerName
./server/?Server=ServerName
(or/server/index.php?Server =ServerName) to
/server/ServerName
(canonical URL)./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: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 precedingcondition
.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 RewriteRulepattern
. 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, theNE
(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. (ThePT
flag is only relevant when using mod_rewrite in the context of a server or virtualhost.)