I’m currently programming a website that, in a nutshell, loads its content dynamically via url parameters and php includes. The website is living under the root directory in a subfolder called “saischek”.
The urls have one optional parameter: page, therefore the urls can for example look like this:
localhost/saischek/index.php?page=accounting
localhost/saischek/index.php
I would like to have that my urls look like this:
localhost/saischek/accounting
localhost/saischek/home <- if the url parameter _page_ isn't given
My .htaccess file is currently living in the subdirectory “saischek” and looks as follows:
RewriteEngine on
RewriteRule ^saischek/([a-zA-Z0-9_-]+)$ saischek/index.php?page=$1
The website is running on apache webserver and all necessary changes in “httpd.conf” file are done and working.
2
Answers
Try adding ? and changing + to * because in case the page is missing, the URL would not have them both.. (both / and page).
^saischek/([a-zA-Z0-9_-]+)/?([a-zA-Z0-9_-]*)$
I’d say with the discussion in the comments to the question and your revision of the question you are nearly there, you found the solution yourself…
I would recommend to slightly change the main rule, also you will need to handle the “home” page you mentioned
I wonder however why you want to use the “home” slug at all, why not simply use
https://example.org/saischek
instead ofhttps://example.org/saischek/home
? In that case you would try this variant:In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported
[END]
flag in your http servers error log file in that case. You can either try to upgrade or use the older[L]
flag, it probably will work the same in this situation, though that depends a bit on your setup.This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (“.htaccess” file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it’s interpretation is enabled at all in the host configuration and that it is located in the host’s
DOCUMENT_ROOT
folder.And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (“.htaccess”). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).