Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/316991-one-rewrite-rule-breaks-other-rules/
Share on other sites

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.

  • 2 weeks later...
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/.

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

 

 

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 by Guldstrand

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.

  • 2 weeks later...

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.

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] 

 

 

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.