1

I have the following code in my .htaccess that is used by Typo3 / Realurl to get speaking urls.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php

So no matter what is written in the URL always the index.php is called. It works perfect, but now I would like to exclude certain paths without creating a folder on the webhost.

So the URL https://www.domain.xy/programs should be passed to the /index.php but the URL https://www.domain.xy/programs/sessions should be passed as /programs/ without session as well without redirecting.


https://www.domain.xy/programs -> index.php -> /programs

https://www.domain.xy/programs/sessions -> index.php -> /programs (without redirect and keeping programs/session in the url)

https://www.domain.xy/weather/today -> index.php -> /weather/today


The reason why I need this, is because there is a VUE Plugin on a Subpage in the CMS that needs its own speaking urls for navigation.

I added the following code to my .htaccess but can't get rid of the redirect.

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^programs/. /programs/ [R,L]

Thank you very much!

Matt
  • 8,758
  • 4
  • 35
  • 64
MaZoli
  • 1,388
  • 1
  • 11
  • 17
  • Does this answer your question? [.htaccess mod\_rewrite - how to exclude directory from rewrite rule](https://stackoverflow.com/questions/1848500/htaccess-mod-rewrite-how-to-exclude-directory-from-rewrite-rule) – CBroe Apr 13 '21 at 14:06
  • First of all move your specific rules **before** the generic ones and verify if it solves your problem. – biesior Apr 13 '21 at 14:11
  • Thank you very much. They are before my generic ones and they work, but they redirect. I need it without the redirect. – MaZoli Apr 13 '21 at 14:13
  • I'm a little bit confused about what you have and what you want. Can you update your question and put the pairs like a rule - URL and what does work and what doesn't? Especially I don't get "*sessions should be passed as programs*" part – biesior Apr 13 '21 at 14:16
  • i added further explanation to the post. i hope this clears things a little. thank you very much – MaZoli Apr 13 '21 at 14:21
  • "`/programs/sessions` -> `index.php` -> `/programs` (without redirect and keeping `programs/session` in the url)" - Not sure what you mean by this? You can't go to `index.php` and then go to `/programs`? But neither is `/programs` a valid endpoint, so it doesn't really make sense as the final target of a "rewrite"? – MrWhite Apr 13 '21 at 14:27
  • well it works for any CMS that is using virtual speaking urls. it should just always rout through index.php and keep the URL in the address bar. both samples work on their own, i just can't connect the two. – MaZoli Apr 13 '21 at 14:30
  • What do you mean by "_speaking_ URLs"? Whether the underlying code (PHP/JS?) sees `/programs` when requesting `/programs/sessions` is really determined by the underlying code, not `.htaccess`. How does your backend read the requested URL? – MrWhite Apr 13 '21 at 14:34
  • i know that i could modify it in php - but i'd like to do it in the htaccess to make it faster and more robust. basically i'd need a way to modify the %{REQUEST_FILENAME} variable – MaZoli Apr 13 '21 at 14:37
  • Depending on how your PHP script reads the requested URL then... if `/programs/sessions` appears in the browser's address bar (as you require) and if PHP is determining the requested URL via `$_SERVER['REQUEST_URI']` (as most do) then PHP is going to still see `/programs/sessions`. (?) However, if your PHP script is written to read the query string or path-info either instead of or as an _overriding feature_ then you could do this in `.htaccess`. So, how is your PHP script reading the requested URL? – MrWhite Apr 13 '21 at 14:42
  • 1
    Well yeah it does use $_SERVER['REQUEST_URI'] and I can't change that. Looks like I am stsuck. Thanks for helping. – MaZoli Apr 13 '21 at 14:44

1 Answers1

-1

You just need the quantifier (example '*') after the regex. Maybe like this:

RewriteRule ^programs/.* /programs/ [R,L]
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • You can use this tool to test it https://htaccess.madewithlove.be/ – josue ponce Apr 13 '21 at 14:10
  • Thank you ver much for your answer. Unfortunately this is redirecting in the browser. I would need a rule that is sending /programs/ to the php but is keepting the /programs/session in the address bar – MaZoli Apr 13 '21 at 14:12
  • @MaZoli you could check this topic https://stackoverflow.com/questions/14895980/htaccess-redirect-without-changing-url – josue ponce Apr 13 '21 at 15:37
  • Adding the `*` quantifier would make no difference (you could just remove the dot). With or without the quantifier it redirects `/programs/sessions` (as stated in the question). However, by now matching 0 or more, instead of just _one_ character (as in the question) it potentially results in a redirect loop (if it wasn't for the filesystem check - assuming this is a physical directory). But, as stated in the question, the OP does not want an external "redirect" - which is the problem they are trying to fix. – MrWhite Apr 13 '21 at 17:08