man, now i want to do that.
All request to URL :
domain.tld/dir/filename.ext will become that :
domain.tld/dir/index.php?p=filename.ext
I tried many times but it's unsuccessful !! :mad: :mad:
pls help me :rolleyes:
thanks !!!

Dani AI

Generated

A few clarifying tips and a safer rewrite pattern you can drop into the /dir/.htaccess to get the exact behavior you asked for (domain.tld/dir/filename.ext -> domain.tld/dir/index.php?p=filename.ext), while avoiding common pitfalls.

As noted, mod_rewrite must be enabled. was right to prevent rewriting index.php to itself; a more robust approach is to skip existing files and directories so images, CSS and the real index.php are left alone. Also watch out for unescaped dots in patterns (use \. to match a literal dot) and for rules that are too greedy.

Recommended .htaccess (place inside /dir/):

RewriteEngine On
RewriteBase /dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?p=$1 [L,QSA]

Notes:

  • Put this file in the actual directory you want rewritten. If you put it in the document root, adjust or remove RewriteBase and/or the pattern accordingly.
  • RewriteCond %{REQUEST_FILENAME} !-f / !-d prevent rewriting real files and folders (avoids loops and breaks).
  • QSA preserves any original query string; L stops further rewriting.
  • To restrict to specific extensions (for example only .php or .html), change the pattern to use \.(php|html)$.

Quick troubleshooting:

  • Confirm mod_rewrite is loaded (apachectl -M or apache2ctl -M) and that AllowOverride All is set for the directory in Apache config.
  • Watch the error log (tail -f /var/log/apache2/error.log) and test requests with curl -I.
  • During testing, avoid R=301 redirects (browsers cache 301s).

For complete syntax and flags, see the official Apache mod_rewrite documentation: mod_rewrite documentation.

Security caution: never include files directly from the p parameter without validating or whitelisting names to prevent local file inclusion.

Recommended Answers

All 4 Replies

The first thing you need is mod_rewrite support by your Apache web server. Unfortunately, I'm not so sure how to do that because I've never had a need to do it before.

Create an .htaccess file that says:

RewriteEngine on
RewriteRule ^(.*).([a-z]+)$ index.php?p=$1.$2 [L]

I BELIEVE something like that should work. I haven't tested it or anything.

i have solved it !!
Thank for help me !!
But i find a diffirent way with you !!

Can you tell us what you did to solve your problem, in case someone else has the same question?

my solution is :

RewriteEngine On
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^/?((.*)/)?(.*)$ $2index.php?page=$3 [L]

it will redirect all request !!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.