Home > OS >  .htaccess pagination on subdomain not working [PHP]
.htaccess pagination on subdomain not working [PHP]

Time:10-03

Let me make it clear, I'm having as base this url domain.com/feedbacks.php?id=exampleuser&page=1

Now using .htaccess I want to rewrite it into this :

exampleuser.domain.com/?page=1

I successfully obtained exampleuser.domain.com but sadly adding & using as pagination the parameter ?page= within my subdomain is not working.

How can I make the the PHP ?page= parameter work using .htaccess?

Or is it any other way that I can simply rewrite domain.com/feedbacks.php?id=exampleuser&page=1 to exampleuser.domain.com/?page=1?

CodePudding user response:

You question is still unclear after the edit, you now dropped important information. So I make give a try here to point out the basics. You probably have to adapt it to your specific needs which are not really clear from your description:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(\w )\.example\.com$
RewriteRule ^ /feedbacks.php?id=%1 [QSA,END]

This obviously assumes that requests to the http host ".example.com" are received and processed at all by your http server. For that you need to setup the correct (wildcard) DNS records and you need to use a default virtual host to catch requests to arbitrary requested host names.

The given rule set can be implemented in the central http server's host configuration for that default host. Or, if you do not have access to that you can instead use a distributed configuration file (".htaccess"), if you enabled that feature before hand (see the documentation of the AllowOverride directive for that).

I kept this as simple as possible to prevent complexity. You need to understand how this works. And I really recommend that you start taking a look into the documentation of the tool you are using, here the http server's rewriting module: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

The rule assumes that the "page=" query parameter is correctly set by the requesting site. If not then it is missing, so your application logic has to handle that case (it will probably return the first page then).

This does _not handle requests to the host "example.com" (you did not specify how). And it also only implements an internal rewrite for incoming requests. You have to take care elsewhere that references (link) you send out already use the hostname that specifies the user id.

  • Related