1

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..

  • In the line that doesn't work you have a ^ that means the beginning of the url, try remove only the ^ sign, because the part you want to replace isn't at the beginning of the url. – Casper Kuethe Jan 07 '22 at 15:35
  • 1
    Where is your htaccess file located? – Amit Verma Jan 07 '22 at 15:41
  • Hi thanks for reply I applied what you said and it changed the error it gives internal server error 500 what can happen on the url? – OĞUZHAN ŞENOKUR Jan 07 '22 at 15:42
  • @AmitVerma it is on with index.php – OĞUZHAN ŞENOKUR Jan 07 '22 at 15:43
  • What folder is it in? – Amit Verma Jan 07 '22 at 15:44
  • did you doublecheck that before and after aciklama/?id=$1 there is a space and not tab character ? – A. Lion Jan 07 '22 at 15:44
  • Thanks for all @CasperKuethe solved my problem the Internal Server Error was cause by aciklama/?id=$1 should be aciklama?id=1 and it works perfectly Thanks again – OĞUZHAN ŞENOKUR Jan 07 '22 at 15:47
  • `admintemplate/baker/urunler/aciklama/1` != `^aciklama/([0-9]*)`, the `^` means from RewriteBase which is / if not set not current folder, so its missing `admintemplate/baker/urunler` part – Lawrence Cherone Jan 07 '22 at 15:48
  • @LawrenceCherone yes I understand clearly thx so much it solved my problem – OĞUZHAN ŞENOKUR Jan 07 '22 at 15:50
  • 1
    _Side note:_ To make your life easier, I would recommend that you look into using the [front controller pattern](https://en.wikipedia.org/wiki/Front_controller) together with a [router](https://packagist.org/?query=router) instead of adding a bunch of application specific rewrite rules in htaccess. Then it's easier to set up new URL's directly in your code, as well as moving the application to different web servers (like nginx etc) – M. Eriksson Jan 07 '22 at 15:51
  • "the ^ means from RewriteBase which is /" - The URL-path that is matched is not dependent on `RewriteBase` (which may not be set at all here). The `^` "means" from wherever the `.htaccess` file is located. If the first rule is "working" then it would seem the `.htaccess` file is located in the `/baker` subdirectory, in which case the regex in the 2nd rule should be `^urunler/aciklama/([0-9]*)$` (only `urunler/` is missing). However, the _substitution_ string is still not complete (probably should be `aciklama/index.php?id=$1`). @LawrenceCherone – MrWhite Jan 07 '22 at 16:03
  • @MrWhite 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? – OĞUZHAN ŞENOKUR Jan 07 '22 at 16:41
  • @OĞUZHANŞENOKUR I've addressed the issue of missing CSS in my answer. – MrWhite Jan 07 '22 at 18:18

1 Answers1

2
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:

MrWhite
  • 43,179
  • 8
  • 60
  • 84