Hide url parameters from the GET request












1















I want to hide some url parameters from the $_GET request using .htaccess. I have tried different approaches and of course from this link: How to hide params passed in url using .htaccess



But it failed for me:



Error image - hidden id



My .htaccess code:



RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
RewriteRule ^([0-9a-zA-Z]+)/?$ /$1.php


But this works only when id is added:



With ID works



I want to hide the id and display it like this:



localhost/get_test.net/test/4



Update:
So, it works with this code:



RewriteBase /
RewriteEngine On
RewriteRule ([^/.]+)/?$ test.php?id=$1 [L]


Now I need to change test.php to any link and fix the issue with index.php page, because it is not found:



Index page not found



Any ideas? Thanks.










share|improve this question

























  • Where is your .htaccess file located?

    – MrWhite
    Nov 25 '18 at 17:33











  • @MrWhite Hi! I use the WAMP for local testing: Screenshot. Thanks.

    – Hunter91151
    Nov 25 '18 at 17:51











  • From your screenshot, it looks like your .htaccess file is located in the /get_test.net subdirectory (the same directory that the test.php script is located). However, your directive rewrites the request back to /test.php in the document root - which would not seem to work, unless you have another test.php in the document root? You say "it works", however, the regex wouldn't match the URL /test/4?

    – MrWhite
    Nov 25 '18 at 18:01













  • @MrWhite I changed the code to: RewriteRule ^test/([0-9]+) test.php?id=$1 [NC,L] but it still fails to get the value from $_GET request: Screen1. I don't have any test.php in the document root: C:wamp64www. I only have it here: `C:wamp64wwwget_test.net`.

    – Hunter91151
    Nov 25 '18 at 18:17
















1















I want to hide some url parameters from the $_GET request using .htaccess. I have tried different approaches and of course from this link: How to hide params passed in url using .htaccess



But it failed for me:



Error image - hidden id



My .htaccess code:



RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
RewriteRule ^([0-9a-zA-Z]+)/?$ /$1.php


But this works only when id is added:



With ID works



I want to hide the id and display it like this:



localhost/get_test.net/test/4



Update:
So, it works with this code:



RewriteBase /
RewriteEngine On
RewriteRule ([^/.]+)/?$ test.php?id=$1 [L]


Now I need to change test.php to any link and fix the issue with index.php page, because it is not found:



Index page not found



Any ideas? Thanks.










share|improve this question

























  • Where is your .htaccess file located?

    – MrWhite
    Nov 25 '18 at 17:33











  • @MrWhite Hi! I use the WAMP for local testing: Screenshot. Thanks.

    – Hunter91151
    Nov 25 '18 at 17:51











  • From your screenshot, it looks like your .htaccess file is located in the /get_test.net subdirectory (the same directory that the test.php script is located). However, your directive rewrites the request back to /test.php in the document root - which would not seem to work, unless you have another test.php in the document root? You say "it works", however, the regex wouldn't match the URL /test/4?

    – MrWhite
    Nov 25 '18 at 18:01













  • @MrWhite I changed the code to: RewriteRule ^test/([0-9]+) test.php?id=$1 [NC,L] but it still fails to get the value from $_GET request: Screen1. I don't have any test.php in the document root: C:wamp64www. I only have it here: `C:wamp64wwwget_test.net`.

    – Hunter91151
    Nov 25 '18 at 18:17














1












1








1


1






I want to hide some url parameters from the $_GET request using .htaccess. I have tried different approaches and of course from this link: How to hide params passed in url using .htaccess



But it failed for me:



Error image - hidden id



My .htaccess code:



RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
RewriteRule ^([0-9a-zA-Z]+)/?$ /$1.php


But this works only when id is added:



With ID works



I want to hide the id and display it like this:



localhost/get_test.net/test/4



Update:
So, it works with this code:



RewriteBase /
RewriteEngine On
RewriteRule ([^/.]+)/?$ test.php?id=$1 [L]


Now I need to change test.php to any link and fix the issue with index.php page, because it is not found:



Index page not found



Any ideas? Thanks.










share|improve this question
















I want to hide some url parameters from the $_GET request using .htaccess. I have tried different approaches and of course from this link: How to hide params passed in url using .htaccess



But it failed for me:



Error image - hidden id



My .htaccess code:



RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
RewriteRule ^([0-9a-zA-Z]+)/?$ /$1.php


But this works only when id is added:



With ID works



I want to hide the id and display it like this:



localhost/get_test.net/test/4



Update:
So, it works with this code:



RewriteBase /
RewriteEngine On
RewriteRule ([^/.]+)/?$ test.php?id=$1 [L]


Now I need to change test.php to any link and fix the issue with index.php page, because it is not found:



Index page not found



Any ideas? Thanks.







.htaccess






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 14:35







Hunter91151

















asked Nov 25 '18 at 11:24









Hunter91151Hunter91151

85




85













  • Where is your .htaccess file located?

    – MrWhite
    Nov 25 '18 at 17:33











  • @MrWhite Hi! I use the WAMP for local testing: Screenshot. Thanks.

    – Hunter91151
    Nov 25 '18 at 17:51











  • From your screenshot, it looks like your .htaccess file is located in the /get_test.net subdirectory (the same directory that the test.php script is located). However, your directive rewrites the request back to /test.php in the document root - which would not seem to work, unless you have another test.php in the document root? You say "it works", however, the regex wouldn't match the URL /test/4?

    – MrWhite
    Nov 25 '18 at 18:01













  • @MrWhite I changed the code to: RewriteRule ^test/([0-9]+) test.php?id=$1 [NC,L] but it still fails to get the value from $_GET request: Screen1. I don't have any test.php in the document root: C:wamp64www. I only have it here: `C:wamp64wwwget_test.net`.

    – Hunter91151
    Nov 25 '18 at 18:17



















  • Where is your .htaccess file located?

    – MrWhite
    Nov 25 '18 at 17:33











  • @MrWhite Hi! I use the WAMP for local testing: Screenshot. Thanks.

    – Hunter91151
    Nov 25 '18 at 17:51











  • From your screenshot, it looks like your .htaccess file is located in the /get_test.net subdirectory (the same directory that the test.php script is located). However, your directive rewrites the request back to /test.php in the document root - which would not seem to work, unless you have another test.php in the document root? You say "it works", however, the regex wouldn't match the URL /test/4?

    – MrWhite
    Nov 25 '18 at 18:01













  • @MrWhite I changed the code to: RewriteRule ^test/([0-9]+) test.php?id=$1 [NC,L] but it still fails to get the value from $_GET request: Screen1. I don't have any test.php in the document root: C:wamp64www. I only have it here: `C:wamp64wwwget_test.net`.

    – Hunter91151
    Nov 25 '18 at 18:17

















Where is your .htaccess file located?

– MrWhite
Nov 25 '18 at 17:33





Where is your .htaccess file located?

– MrWhite
Nov 25 '18 at 17:33













@MrWhite Hi! I use the WAMP for local testing: Screenshot. Thanks.

– Hunter91151
Nov 25 '18 at 17:51





@MrWhite Hi! I use the WAMP for local testing: Screenshot. Thanks.

– Hunter91151
Nov 25 '18 at 17:51













From your screenshot, it looks like your .htaccess file is located in the /get_test.net subdirectory (the same directory that the test.php script is located). However, your directive rewrites the request back to /test.php in the document root - which would not seem to work, unless you have another test.php in the document root? You say "it works", however, the regex wouldn't match the URL /test/4?

– MrWhite
Nov 25 '18 at 18:01







From your screenshot, it looks like your .htaccess file is located in the /get_test.net subdirectory (the same directory that the test.php script is located). However, your directive rewrites the request back to /test.php in the document root - which would not seem to work, unless you have another test.php in the document root? You say "it works", however, the regex wouldn't match the URL /test/4?

– MrWhite
Nov 25 '18 at 18:01















@MrWhite I changed the code to: RewriteRule ^test/([0-9]+) test.php?id=$1 [NC,L] but it still fails to get the value from $_GET request: Screen1. I don't have any test.php in the document root: C:wamp64www. I only have it here: `C:wamp64wwwget_test.net`.

– Hunter91151
Nov 25 '18 at 18:17





@MrWhite I changed the code to: RewriteRule ^test/([0-9]+) test.php?id=$1 [NC,L] but it still fails to get the value from $_GET request: Screen1. I don't have any test.php in the document root: C:wamp64www. I only have it here: `C:wamp64wwwget_test.net`.

– Hunter91151
Nov 25 '18 at 18:17












1 Answer
1






active

oldest

votes


















0














If your file structure is like this (from your screenshot):



/get_test.net
.htaccess
test.php
index.php


Then to rewrite /get_test.net/<file>/<id> to /get_test.net/<file>.php?id=<id>, where <id> is numeric and /<id> is optional then you would need to do something like:



# Prevent conflict with mod_negotiation
Options -MultiViews

RewriteEngine On

# Do NOT set RewriteBase (or set it to the subdirectory)
#RewriteBase /get_test.net

# Rewrite /<file>/<id> to <file>.php?id=<id>
RewriteRule ^([^/.]+)(?:/(d+))?$ $1.php?id=$2 [L]


There is no need to escape the literal dot when used in a regex character class.



UPDATE: The above also handles the situation when the trailing /<id> is omitted from the URL. eg. /index is rewritten to /index.php?id= (not the empty URL param). Note also that if the trailing slash is provided then the ID must also consist of 1 or more digits.






share|improve this answer


























  • All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

    – Hunter91151
    Nov 25 '18 at 18:26











  • Ah, so /<id> is optional?

    – MrWhite
    Nov 25 '18 at 18:30











  • Yes, for example: index - main page test?id=XX - navigation from index (main page)

    – Hunter91151
    Nov 25 '18 at 18:33













  • I've updated my answer.

    – MrWhite
    Nov 25 '18 at 22:59






  • 1





    Ok. All works fine. Thank you.

    – Hunter91151
    Nov 26 '18 at 19:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466963%2fhide-url-parameters-from-the-get-request%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If your file structure is like this (from your screenshot):



/get_test.net
.htaccess
test.php
index.php


Then to rewrite /get_test.net/<file>/<id> to /get_test.net/<file>.php?id=<id>, where <id> is numeric and /<id> is optional then you would need to do something like:



# Prevent conflict with mod_negotiation
Options -MultiViews

RewriteEngine On

# Do NOT set RewriteBase (or set it to the subdirectory)
#RewriteBase /get_test.net

# Rewrite /<file>/<id> to <file>.php?id=<id>
RewriteRule ^([^/.]+)(?:/(d+))?$ $1.php?id=$2 [L]


There is no need to escape the literal dot when used in a regex character class.



UPDATE: The above also handles the situation when the trailing /<id> is omitted from the URL. eg. /index is rewritten to /index.php?id= (not the empty URL param). Note also that if the trailing slash is provided then the ID must also consist of 1 or more digits.






share|improve this answer


























  • All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

    – Hunter91151
    Nov 25 '18 at 18:26











  • Ah, so /<id> is optional?

    – MrWhite
    Nov 25 '18 at 18:30











  • Yes, for example: index - main page test?id=XX - navigation from index (main page)

    – Hunter91151
    Nov 25 '18 at 18:33













  • I've updated my answer.

    – MrWhite
    Nov 25 '18 at 22:59






  • 1





    Ok. All works fine. Thank you.

    – Hunter91151
    Nov 26 '18 at 19:24
















0














If your file structure is like this (from your screenshot):



/get_test.net
.htaccess
test.php
index.php


Then to rewrite /get_test.net/<file>/<id> to /get_test.net/<file>.php?id=<id>, where <id> is numeric and /<id> is optional then you would need to do something like:



# Prevent conflict with mod_negotiation
Options -MultiViews

RewriteEngine On

# Do NOT set RewriteBase (or set it to the subdirectory)
#RewriteBase /get_test.net

# Rewrite /<file>/<id> to <file>.php?id=<id>
RewriteRule ^([^/.]+)(?:/(d+))?$ $1.php?id=$2 [L]


There is no need to escape the literal dot when used in a regex character class.



UPDATE: The above also handles the situation when the trailing /<id> is omitted from the URL. eg. /index is rewritten to /index.php?id= (not the empty URL param). Note also that if the trailing slash is provided then the ID must also consist of 1 or more digits.






share|improve this answer


























  • All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

    – Hunter91151
    Nov 25 '18 at 18:26











  • Ah, so /<id> is optional?

    – MrWhite
    Nov 25 '18 at 18:30











  • Yes, for example: index - main page test?id=XX - navigation from index (main page)

    – Hunter91151
    Nov 25 '18 at 18:33













  • I've updated my answer.

    – MrWhite
    Nov 25 '18 at 22:59






  • 1





    Ok. All works fine. Thank you.

    – Hunter91151
    Nov 26 '18 at 19:24














0












0








0







If your file structure is like this (from your screenshot):



/get_test.net
.htaccess
test.php
index.php


Then to rewrite /get_test.net/<file>/<id> to /get_test.net/<file>.php?id=<id>, where <id> is numeric and /<id> is optional then you would need to do something like:



# Prevent conflict with mod_negotiation
Options -MultiViews

RewriteEngine On

# Do NOT set RewriteBase (or set it to the subdirectory)
#RewriteBase /get_test.net

# Rewrite /<file>/<id> to <file>.php?id=<id>
RewriteRule ^([^/.]+)(?:/(d+))?$ $1.php?id=$2 [L]


There is no need to escape the literal dot when used in a regex character class.



UPDATE: The above also handles the situation when the trailing /<id> is omitted from the URL. eg. /index is rewritten to /index.php?id= (not the empty URL param). Note also that if the trailing slash is provided then the ID must also consist of 1 or more digits.






share|improve this answer















If your file structure is like this (from your screenshot):



/get_test.net
.htaccess
test.php
index.php


Then to rewrite /get_test.net/<file>/<id> to /get_test.net/<file>.php?id=<id>, where <id> is numeric and /<id> is optional then you would need to do something like:



# Prevent conflict with mod_negotiation
Options -MultiViews

RewriteEngine On

# Do NOT set RewriteBase (or set it to the subdirectory)
#RewriteBase /get_test.net

# Rewrite /<file>/<id> to <file>.php?id=<id>
RewriteRule ^([^/.]+)(?:/(d+))?$ $1.php?id=$2 [L]


There is no need to escape the literal dot when used in a regex character class.



UPDATE: The above also handles the situation when the trailing /<id> is omitted from the URL. eg. /index is rewritten to /index.php?id= (not the empty URL param). Note also that if the trailing slash is provided then the ID must also consist of 1 or more digits.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 22:58

























answered Nov 25 '18 at 18:16









MrWhiteMrWhite

12.9k33162




12.9k33162













  • All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

    – Hunter91151
    Nov 25 '18 at 18:26











  • Ah, so /<id> is optional?

    – MrWhite
    Nov 25 '18 at 18:30











  • Yes, for example: index - main page test?id=XX - navigation from index (main page)

    – Hunter91151
    Nov 25 '18 at 18:33













  • I've updated my answer.

    – MrWhite
    Nov 25 '18 at 22:59






  • 1





    Ok. All works fine. Thank you.

    – Hunter91151
    Nov 26 '18 at 19:24



















  • All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

    – Hunter91151
    Nov 25 '18 at 18:26











  • Ah, so /<id> is optional?

    – MrWhite
    Nov 25 '18 at 18:30











  • Yes, for example: index - main page test?id=XX - navigation from index (main page)

    – Hunter91151
    Nov 25 '18 at 18:33













  • I've updated my answer.

    – MrWhite
    Nov 25 '18 at 22:59






  • 1





    Ok. All works fine. Thank you.

    – Hunter91151
    Nov 26 '18 at 19:24

















All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

– Hunter91151
Nov 25 '18 at 18:26





All good, but index page was not found because I need to hide .php extension. Previously I used this code: RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php But now it doesn't work: Screen2

– Hunter91151
Nov 25 '18 at 18:26













Ah, so /<id> is optional?

– MrWhite
Nov 25 '18 at 18:30





Ah, so /<id> is optional?

– MrWhite
Nov 25 '18 at 18:30













Yes, for example: index - main page test?id=XX - navigation from index (main page)

– Hunter91151
Nov 25 '18 at 18:33







Yes, for example: index - main page test?id=XX - navigation from index (main page)

– Hunter91151
Nov 25 '18 at 18:33















I've updated my answer.

– MrWhite
Nov 25 '18 at 22:59





I've updated my answer.

– MrWhite
Nov 25 '18 at 22:59




1




1





Ok. All works fine. Thank you.

– Hunter91151
Nov 26 '18 at 19:24





Ok. All works fine. Thank you.

– Hunter91151
Nov 26 '18 at 19:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466963%2fhide-url-parameters-from-the-get-request%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft