Guldstrand Posted November 26, 2022 Share Posted November 26, 2022 Options +FollowSymLinks -MultiViews RewriteEngine On ErrorDocument 404 /404.php RewriteCond %{HTTP_HOST} ^domain.com$ RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L] RewriteRule ^(.*)/ page.php?slug=$1 [L,QSA] I have some kind of a "conflict" with the code above in my htaccess-file. When i add the rule to have all page.php?slug=$pagename to domain.com/$pagename, the other rewritten page URLs breaks. I'm not completely familiar with mod_rewrite yet, but I'm trying to understand. I would be extremely grateful if someone could help me with this. Thanks in advance.. Quote Link to comment https://forums.phpfreaks.com/topic/315585-problem-with-rewriterule/ Share on other sites More sharing options...
gizmola Posted November 26, 2022 Share Posted November 26, 2022 I'm not 100% sure what you trying to do. I tried to extrapolate that you are looking for "/something" and wanting to rewrite that to "/page.php?slug=something". Options +FollowSymLinks -MultiViews RewriteEngine On ErrorDocument 404 /404.php RewriteCond %{HTTP_HOST} ^domain.com$ RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L] RewriteCond %{HTTP_HOST} !php$ RewriteRule "^([A-Za-z]+)" "page.php?slug=$1" [L,QSA] Since you didn't provide examples of what other rules this "broke", this is my best guess. An important addition is that this rule won't run for existing *.php scripts. Quote Link to comment https://forums.phpfreaks.com/topic/315585-problem-with-rewriterule/#findComment-1602967 Share on other sites More sharing options...
Guldstrand Posted November 27, 2022 Author Share Posted November 27, 2022 (edited) I fetch a "page" (content) dynamically from mysql, which is then displayed at domain.com/page.php?slug=pagename. There are several other rewrite rules that work, such as domain.com/news/$id/$title/. This is the only one that doesn't work as it is right after the domain? (domain.com/$pagename/) Again, all other rules are working: RewriteRule ^news/([0-9]+)/(.*)/ news-article.php?id=$1&title=$2 [L,QSA] Edited November 27, 2022 by Guldstrand Quote Link to comment https://forums.phpfreaks.com/topic/315585-problem-with-rewriterule/#findComment-1602994 Share on other sites More sharing options...
gizmola Posted December 2, 2022 Share Posted December 2, 2022 I provided you a solution that should work. Just keep in mind that rules are processed in order. You need this "catchall" to come at the end/after all the other more specific rules or those other rules will never be reached. So you want: www rewrite rule specific pseudo directory rules like your news/ the catchall to *.php Just looking at where you are going with this, having a routing class and using a front controller pattern where you route everything through index.php which acts as a router, is essentially the direction you are taking your site from the look of it. You can make everything simpler and cleaner by either porting to symfony or laravel (which have many benefits in terms of access to all the other features of those frameworks) or just integrate some routing class into what you have. This looks like a possible solution for you: https://phprouter.com/ With that said there are myriad others, but that one appears to be functional, simple and minimal. Quote Link to comment https://forums.phpfreaks.com/topic/315585-problem-with-rewriterule/#findComment-1603188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.