Home > OS >  htaccess URL rewriting PHP for posts and offers
htaccess URL rewriting PHP for posts and offers

Time:12-21

i've searched for a long time an answer for my issue, I found a lot of ideas, but I can't figure it out and make it work as I expect..

So, I've a website with

  • "/index.php"
  • "/posts.php"

What I want is to rewrite the url in order to : redirect "/index.php", "/index.php/" and "/index/" to "/"

and also :

redirect "/posts/slug-post-1/" to "offers.php" but still display "/posts/slug-post-1/" in which I would split the url to get the slug of the post.

Thanks

What I've tried is :

## To internally redirect /dir/foo to /dir/foo.php

RewriteCond %{REQUEST_FILENAME}.php -f [NC]

RewriteRule ^ %{REQUEST_URI}.php [L]

It actually display "/offers/" and redirect to "/offers.php" but when i add a post slug, it doesn't work.

So I've also tried : RewriteRule ^posts/([^/]*)$ /posts.php?slug=$1 [L]

It works only with ctrl F5 and not a simple refresh.. I don't understand why. This is the same with different brwoser and computer.

CodePudding user response:

I'll give it a shot :-)

To redirect "/index.php", "/index.php/" and "/index/" to "/".

RewriteRule index(.*)$ / [NC, L]

Edit: I really urge you not to change the default behaviour of a crucial file like index.php in main folder. There will be unforeseen consequences.

And the other one:

RewriteRule posts/slug-post-1(.*)$ offers.php [QSA, NC, L]

In your example:

Your RewriteCond %{REQUEST_FILENAME}.php -f [NC] checks if "requests filename" with .php added is a real existing file.

Use RewriteCond ${REQUEST_FILENAME} !-f to make sure the targeted file is not skipped, although it is existing. !-d would check for directories.

If you want slugs to be added dynamically to the new target, use flag QSA, so [QSA, NC, L] instead of [NC, L], for example. Use the condition before the rule.

CodePudding user response:

This is really a comment - but space is limited.

It looks as if you haven't thought through what you are trying to achieve before trying to implement it.

What I want is to rewrite the url in order to : redirect "/index.php", "/index.php/" and "/index/" to "/"

I think you need to do a lot more searching. Webservers don't serve up directories, they typically have a lot of machinery in place to service up content when presented with a request where the path maps to a directory to change that to a file, a script, or a special handler.

I suspect you want to rewrite /, /index.php/ and /index/ to /index.php

But I suspect there's more to what you are trying to achieve here - that you also want to deal with any string after the pattern you are seeking to match which is implied in your attempts to deal with /posts.

So it looks as if you are trying to implement 2 front controller patterns. Implementing a single front controller pattern appears to be a bit of a stretch for you. Implementing 2 at the same time is unlikely to turn out well and whatever you do finally implement will likely be very fragile. You're going to need a router in /index.php so that is the right place to handle the /posts/ requests.

But this is only PART of the problem you need to solve. Having your PHP code intercepting all requests is rather expensive in terms of CPU and memory (unless you have a really good caching policy implemented on your server and it is sitting behind a caching reverse proxy).

  • Related