Using htaccess modrewrite for friendly urls gives a 404 error










0














I am trying to rewrite



http://example.com/category/this-is-my-category



to...



http://example.com/category.php?id=this-is-my-category



My .htaccess file is below:



Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule ^category/(d+)/([w-]+)$ /category.php?id=$1 [L]


This gives a 404 error



http://example.com/category.php exists on the server



I have also tried



RewriteRule ^category/(d+)/([w-]+)$ ./category.php?id=$1 [L]


and



RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1


I have read some articles on this and can't see an issue with the code in the .htaccess file.










share|improve this question



















  • 1




    FYI, it's actually the other way around... you want to rewrite the nice URL to the real one
    – Phil
    Nov 12 '18 at 22:13










  • What URL are you using to test? Is it literally /category/this-is-my-category?
    – Phil
    Nov 12 '18 at 22:15










  • Thanks. im using... example.com/category/this-is-my-category to test
    – user606336
    Nov 12 '18 at 22:20















0














I am trying to rewrite



http://example.com/category/this-is-my-category



to...



http://example.com/category.php?id=this-is-my-category



My .htaccess file is below:



Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule ^category/(d+)/([w-]+)$ /category.php?id=$1 [L]


This gives a 404 error



http://example.com/category.php exists on the server



I have also tried



RewriteRule ^category/(d+)/([w-]+)$ ./category.php?id=$1 [L]


and



RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1


I have read some articles on this and can't see an issue with the code in the .htaccess file.










share|improve this question



















  • 1




    FYI, it's actually the other way around... you want to rewrite the nice URL to the real one
    – Phil
    Nov 12 '18 at 22:13










  • What URL are you using to test? Is it literally /category/this-is-my-category?
    – Phil
    Nov 12 '18 at 22:15










  • Thanks. im using... example.com/category/this-is-my-category to test
    – user606336
    Nov 12 '18 at 22:20













0












0








0







I am trying to rewrite



http://example.com/category/this-is-my-category



to...



http://example.com/category.php?id=this-is-my-category



My .htaccess file is below:



Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule ^category/(d+)/([w-]+)$ /category.php?id=$1 [L]


This gives a 404 error



http://example.com/category.php exists on the server



I have also tried



RewriteRule ^category/(d+)/([w-]+)$ ./category.php?id=$1 [L]


and



RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1


I have read some articles on this and can't see an issue with the code in the .htaccess file.










share|improve this question















I am trying to rewrite



http://example.com/category/this-is-my-category



to...



http://example.com/category.php?id=this-is-my-category



My .htaccess file is below:



Options +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule ^category/(d+)/([w-]+)$ /category.php?id=$1 [L]


This gives a 404 error



http://example.com/category.php exists on the server



I have also tried



RewriteRule ^category/(d+)/([w-]+)$ ./category.php?id=$1 [L]


and



RewriteRule ^/category/([a-zA-Z0-9]+)$ /category.php?id=$1


I have read some articles on this and can't see an issue with the code in the .htaccess file.







php regex .htaccess mod-rewrite friendly-url






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:27

























asked Nov 12 '18 at 22:11









user606336

176




176







  • 1




    FYI, it's actually the other way around... you want to rewrite the nice URL to the real one
    – Phil
    Nov 12 '18 at 22:13










  • What URL are you using to test? Is it literally /category/this-is-my-category?
    – Phil
    Nov 12 '18 at 22:15










  • Thanks. im using... example.com/category/this-is-my-category to test
    – user606336
    Nov 12 '18 at 22:20












  • 1




    FYI, it's actually the other way around... you want to rewrite the nice URL to the real one
    – Phil
    Nov 12 '18 at 22:13










  • What URL are you using to test? Is it literally /category/this-is-my-category?
    – Phil
    Nov 12 '18 at 22:15










  • Thanks. im using... example.com/category/this-is-my-category to test
    – user606336
    Nov 12 '18 at 22:20







1




1




FYI, it's actually the other way around... you want to rewrite the nice URL to the real one
– Phil
Nov 12 '18 at 22:13




FYI, it's actually the other way around... you want to rewrite the nice URL to the real one
– Phil
Nov 12 '18 at 22:13












What URL are you using to test? Is it literally /category/this-is-my-category?
– Phil
Nov 12 '18 at 22:15




What URL are you using to test? Is it literally /category/this-is-my-category?
– Phil
Nov 12 '18 at 22:15












Thanks. im using... example.com/category/this-is-my-category to test
– user606336
Nov 12 '18 at 22:20




Thanks. im using... example.com/category/this-is-my-category to test
– user606336
Nov 12 '18 at 22:20












1 Answer
1






active

oldest

votes


















4














Right, so your first regex...



^category/(d+)/([w-]+)$


requires a number between category and the last part, eg /category/1234/something-else.



Your second regex...



^/category/([a-zA-Z0-9]+)$


has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.



The URL you're testing has letters and hyphens.



To me, it looks like you want



RewriteEngine On

RewriteRule ^category/([w-]+)$ /category.php?id=$1 [L,QSA]


Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e




Some notes...



  • It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it


  • I've added the QSA flag so any query parameters are preserved. For example



    /category/this-is-my-category?foo=bar


    becomes



    /category.php?id=this-is-my-category&foo=bar






share|improve this answer






















  • Thanks. Works perfectly. Im am now going to study up a bit
    – user606336
    Nov 12 '18 at 22:45










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%2f53270832%2fusing-htaccess-modrewrite-for-friendly-urls-gives-a-404-error%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









4














Right, so your first regex...



^category/(d+)/([w-]+)$


requires a number between category and the last part, eg /category/1234/something-else.



Your second regex...



^/category/([a-zA-Z0-9]+)$


has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.



The URL you're testing has letters and hyphens.



To me, it looks like you want



RewriteEngine On

RewriteRule ^category/([w-]+)$ /category.php?id=$1 [L,QSA]


Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e




Some notes...



  • It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it


  • I've added the QSA flag so any query parameters are preserved. For example



    /category/this-is-my-category?foo=bar


    becomes



    /category.php?id=this-is-my-category&foo=bar






share|improve this answer






















  • Thanks. Works perfectly. Im am now going to study up a bit
    – user606336
    Nov 12 '18 at 22:45















4














Right, so your first regex...



^category/(d+)/([w-]+)$


requires a number between category and the last part, eg /category/1234/something-else.



Your second regex...



^/category/([a-zA-Z0-9]+)$


has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.



The URL you're testing has letters and hyphens.



To me, it looks like you want



RewriteEngine On

RewriteRule ^category/([w-]+)$ /category.php?id=$1 [L,QSA]


Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e




Some notes...



  • It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it


  • I've added the QSA flag so any query parameters are preserved. For example



    /category/this-is-my-category?foo=bar


    becomes



    /category.php?id=this-is-my-category&foo=bar






share|improve this answer






















  • Thanks. Works perfectly. Im am now going to study up a bit
    – user606336
    Nov 12 '18 at 22:45













4












4








4






Right, so your first regex...



^category/(d+)/([w-]+)$


requires a number between category and the last part, eg /category/1234/something-else.



Your second regex...



^/category/([a-zA-Z0-9]+)$


has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.



The URL you're testing has letters and hyphens.



To me, it looks like you want



RewriteEngine On

RewriteRule ^category/([w-]+)$ /category.php?id=$1 [L,QSA]


Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e




Some notes...



  • It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it


  • I've added the QSA flag so any query parameters are preserved. For example



    /category/this-is-my-category?foo=bar


    becomes



    /category.php?id=this-is-my-category&foo=bar






share|improve this answer














Right, so your first regex...



^category/(d+)/([w-]+)$


requires a number between category and the last part, eg /category/1234/something-else.



Your second regex...



^/category/([a-zA-Z0-9]+)$


has an incorrect leading slash (rewrite rules start at the rewrite-base) and requires only letters and numbers after category, eg /category/thisIsMyCategory.



The URL you're testing has letters and hyphens.



To me, it looks like you want



RewriteEngine On

RewriteRule ^category/([w-]+)$ /category.php?id=$1 [L,QSA]


Demo ~ https://htaccess.madewithlove.be?share=8c3de5aa-68f3-5ec0-9c69-23ff2dbe2d6e




Some notes...



  • It's rare to ever need RewriteBase, especially if your .htaccess file is in the root directory so I've removed it


  • I've added the QSA flag so any query parameters are preserved. For example



    /category/this-is-my-category?foo=bar


    becomes



    /category.php?id=this-is-my-category&foo=bar







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 22:46

























answered Nov 12 '18 at 22:41









Phil

95.8k11136155




95.8k11136155











  • Thanks. Works perfectly. Im am now going to study up a bit
    – user606336
    Nov 12 '18 at 22:45
















  • Thanks. Works perfectly. Im am now going to study up a bit
    – user606336
    Nov 12 '18 at 22:45















Thanks. Works perfectly. Im am now going to study up a bit
– user606336
Nov 12 '18 at 22:45




Thanks. Works perfectly. Im am now going to study up a bit
– user606336
Nov 12 '18 at 22:45

















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53270832%2fusing-htaccess-modrewrite-for-friendly-urls-gives-a-404-error%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

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党