Guldstrand Posted June 21, 2023 Share Posted June 21, 2023 I need to rewrite urls from page.php?slug=name to domain.com/name/, but every try is breaking all other rules in my htaccess-file. Here is a bit from my htaccess-file: Options +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{REQUEST_URI} !(/$|\.) RewriteRule (.*) %{REQUEST_URI}/ [R=301] ErrorDocument 404 /404.php RewriteCond %{HTTP_HOST} ^domain.com$ RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L] RewriteRule ^contact/ contact.php [L,QSA] .... RewriteRule ^news/([0-9]+)/(.*)/ view-article.php?id=$1&title=$2 [L,QSA] RewriteRule ^(.*)/ page.php?slug=$1 [L,QSA] I'm still trying to learn, and would appreciate any help solving this, and also optimizing the other rules, if needed? Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/ Share on other sites More sharing options...
requinix Posted June 21, 2023 Share Posted June 21, 2023 Your rules say that every single thing containing a slash needs to be rewritten to page.php. Are you sure you don't mean that every single thing containing a slash which does not exist should be rewritten? Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1609883 Share on other sites More sharing options...
gizmola Posted June 21, 2023 Share Posted June 21, 2023 2 hours ago, requinix said: Your rules say that every single thing containing a slash needs to be rewritten to page.php. Are you sure you don't mean that every single thing containing a slash which does not exist should be rewritten? Indeed, your pattern should be something like RewriteRule ^page/(.*) /page.php?slug=$1 [L,QSA] Take care with the ^ and $ anchors as they may be more restrictive than you actually want. Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1609888 Share on other sites More sharing options...
Guldstrand Posted July 4, 2023 Author Share Posted July 4, 2023 On 6/22/2023 at 12:10 AM, gizmola said: Indeed, your pattern should be something like RewriteRule ^page/(.*) /page.php?slug=$1 [L,QSA] Take care with the ^ and $ anchors as they may be more restrictive than you actually want. Thanks, but no, that's not how I want it. That way works well for articles, events, posts, etc. I need a rewrite rule that redirects all dynamic pages to to domain.com/slugname/. Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610258 Share on other sites More sharing options...
gizmola Posted July 4, 2023 Share Posted July 4, 2023 I don't think you understand rewrites. Rewriting in this way allows you to use a non-existent url like /slugname/ to the scripts that can actually process them. You can't "rewrite" the actual scripts to a non-existent url. They could never be resolved. Rewrites are not magical "rewrite my url's to the rest of the world on the fly". It's the responsibility of your markup and code to display your url's in the virtual/ pre-rewritten form you want users to see them. If I've misunderstood your statement, please let respond with a specific example as in: Client sends https://www.domain.com/a/ ---> Server rewrites to https://www.domain.com/page.php?slug=a Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610269 Share on other sites More sharing options...
Guldstrand Posted July 7, 2023 Author Share Posted July 7, 2023 (edited) Yes, I know how it works.It may be me explaining it a bit clumsily. As I said, I already have rules that sends: domain.com/article.php?id/slug=$variable --> domain.com/articles/$variable/ domain.com/event.php?id/slug=$variable --> domain.com/events/$variable/ ...and so on, and that works without any issues. But now I need a solution where I can put all the subpages (which are created dynamically and fetched from mysql) directly under domain.com/subpage/. I know that it should be possible to do this, because among other things Wordpress has this possibility. And I don't know what I have done now, because now you are not sent to a 404 page when an address does not exist. (Been sitting with this for so long now, and may have gone blind.) You can just keep adding characters to the link, and you stay on the same page, and are not forwarded to the 404 page, which worked earlier. 😩 I'm attaching my htaccess file, and I'd be super grateful for any help improving/optimizing it. htaccess.txt Edited July 7, 2023 by Guldstrand Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610320 Share on other sites More sharing options...
requinix Posted July 7, 2023 Share Posted July 7, 2023 If we're still talking about the original issue, my reply On 6/21/2023 at 12:32 PM, requinix said: Your rules say that every single thing containing a slash needs to be rewritten to page.php. Are you sure you don't mean that every single thing containing a slash which does not exist should be rewritten? was trying to point out that you were rewriting everything when you should be rewriting only the things that don't exist. Also, 2 hours ago, Guldstrand said: Yes, I know how it works. followed by 2 hours ago, Guldstrand said: As I said, I already have rules that sends: domain.com/article.php?id/slug=$variable --> domain.com/articles/$variable/ domain.com/event.php?id/slug=$variable --> domain.com/events/$variable/ is funny. Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610326 Share on other sites More sharing options...
Guldstrand Posted July 19, 2023 Author Share Posted July 19, 2023 Sorry, but I don't really understand what you mean by this? I just want an solution that rewrites a variable (slug) directly to domain.com/$variable/, without this breaking any of the other rules that exist. I know that this possibility should exist, because it can be done in e.g. Wordpress. But the ways I've tried in the past, to achieve this, have always broken the other rules. Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610605 Share on other sites More sharing options...
requinix Posted July 19, 2023 Share Posted July 19, 2023 Once again: On 7/6/2023 at 7:54 PM, requinix said: you should be rewriting only the things that don't exist I'll even point you in the direction of using RewriteCond to perform that check. Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610607 Share on other sites More sharing options...
gizmola Posted July 22, 2023 Share Posted July 22, 2023 This is the mod_rewrite rules of a typical current wordpress: RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] What this shows, is that Wordpress implements a front controller. I don't know exactly what the issue is, because my example was: user's url -> rewritten to And it appears to me that you presented: rewritten to -> user's url Requinix has tried to steer you towards this, so I'm going back somewhat to the start, and suggesting you add this to the bottom of your rules: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/ page.php?slug=$1 [L,QSA] Quote Link to comment https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/#findComment-1610664 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.