OĞUZHAN ŞENOKUR
OĞUZHAN ŞENOKUR

Reputation: 31

Why I get 404 not found with the .htaccess RewriteRule?

The following codes

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-_]+)$ index.php?page=$1 [L,QSA]// works
RewriteRule ^aciklama/([0-9]*)$      aciklama/?id=$1    [NC,L]//does not works


RewriteRule ^$ index.php [L,QSA]

It works for pages but when I want to pass parameter to another page it gives 404 not found

Step 1-http://localhost/admintemplate/baker/urunler the url is products page and it shows product Step 2-When I clicked to one product for example ID=1 product it will be http://localhost/admintemplate/baker/urunler/aciklama/1 but it does not show the page it says 404 not found.. What can be problem

Thanks and Best regards..

Upvotes: 1

Views: 1063

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-_]+)$ index.php?page=$1 [L,QSA]// works
RewriteRule ^aciklama/([0-9]*)$      aciklama/?id=$1    [NC,L]//does not works

If the first rule is "working" when you request http://localhost/admintemplate/baker/urunler then it would seem the .htaccess file is located in the /admintemplate/baker subdirectory, in which case the regex in the 2nd rule should be ^urunler/aciklama/([0-9]*)$ (not ^aciklama/([0-9]*)$ as stated). Although you probably want to be matching 1 or more digits, not 0 or more and allowing an empty id parameter. So, this regex should probably be ^urunler/aciklama/([0-9]+)$. And this will help avoid a rewrite-loop if the digits were omitted.

However, the substitution string aciklama/?id=$1 is still not complete, since this is not a valid end-point (it is perhaps reliant on mod_dir and the DirectoryIndex). This should probably be aciklama/index.php?id=$1.

The first condition (RewriteCond directive) that checks that the request does not map to a file is not required since the regex ^([0-9a-zA-Z-_]+)$ would not match a file anyway. The two RewriteCond directives only apply to the first RewriteRule. The regex ^([0-9a-zA-Z-_]+)$ is not strictly correct either. The second to last hyphen in the character class needs to be backslash-escaped, or moved to the last (or first) position in order to avoid ambiguity. However, this regex could be simplified by using the \w (word char) shorthand character class. ie. ^([\w-]+)$.

Try the following instead:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?page=$1 [L,QSA]
RewriteRule ^urunler/aciklama/([0-9]+)$ aciklama/index.php?id=$1 [NC,L]
RewriteRule ^$ index.php [L,QSA]

This rule should be unnecessary, providing DirectoryIndex is set correctly (which it must be if the target in the preceding rule was valid). Otherwise, this should be set at the top of the file:

DirectoryIndex index.php

There is also new problem which aciklama/1 page comes with non styled I mean the design is not there only white and texts are there how can I fix it?

This is most probably caused by using relative URL-path to your CSS files (and possibly other static resources).

By default, the browser resolves relative URLs relative to the current URL (in the browser). So, a relative URL like href="mystyle.css" then this is going to be resolved relative to aciklama/1, eg. aciklama/mystyle.css - which is probably not the intention.

If you are rewriting the URL to different path depths then you need to use root-relative (starting with a slash) or absolute (with a scheme + hostname) URLs so the browser is able to resolve the URL correctly. As a workaround you can also use the base element in the head section to indicate the base URL that any relative URLs should be relative too.

See the following question on the Webmasters stack that goes into more detail regarding the use of relative URLs when URL-rewriting:

Upvotes: 2

Related Questions