Using htaccess modrewrite for friendly urls gives a 404 error
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
add a comment |
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
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
add a comment |
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
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
php regex .htaccess mod-rewrite friendly-url
edited Nov 12 '18 at 22:27
user606336
asked Nov 12 '18 at 22:11
user606336user606336
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
Thanks. Works perfectly. Im am now going to study up a bit
– user606336
Nov 12 '18 at 22:45
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thanks. Works perfectly. Im am now going to study up a bit
– user606336
Nov 12 '18 at 22:45
add a comment |
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
Thanks. Works perfectly. Im am now going to study up a bit
– user606336
Nov 12 '18 at 22:45
add a comment |
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
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
edited Nov 12 '18 at 22:46
answered Nov 12 '18 at 22:41
PhilPhil
96.6k11136157
96.6k11136157
Thanks. Works perfectly. Im am now going to study up a bit
– user606336
Nov 12 '18 at 22:45
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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