Jump to content

Problem with RewriteRule


Guldstrand

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Guldstrand
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.