I want url like www.abc.com/pagename but when I wrote RewriteRule ^(.*)$ page-detail.php?url=$1 but it will show www.abc.com/pagename not www.abc.com/index.php or www.abc.com

I want to run both pages www.abc.com/index.php and www.abc.com/pagename. How I can ?

Dani AI

Generated

's problem comes from the rule ^(.*)$ being greedy: it matches the empty string, so the site root gets rewritten to page-detail.php and index.php never runs. A compact, reliable pattern is to (a) let existing files and folders be served as-is and (b) only rewrite non-empty “pretty” paths. The snippet below implements that and preserves query strings.

RewriteEngine On
RewriteBase /

# serve existing files and directories normally
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# do not rewrite explicit requests for index.php or the root
RewriteRule ^(index\.php)?$ - [L]

# route non-empty pretty URLs to page-detail.php (preserve query string)
RewriteRule ^(.+?)/?$ page-detail.php?url=$1 [L,QSA]

Notes and alternatives:

  • The key change vs ^(.*)$ is ^(.+?)/?$ which requires at least one character, so the root (empty path) is not matched.
  • If pretty URLs should be a single segment only (no slashes), use ^([^/]+)/?$ to capture just the first part.
  • QSA preserves any incoming query string; L stops further rewriting.
  • To exempt specific asset folders (images, css, etc.), add an early rule like RewriteRule ^(assets|static|media)/ - [L] before the catch-all.

Relation to earlier replies:

  • ’s idea to check for PHP files can be useful for mapping pretty names to existing .php files, but requiring a trailing slash is fragile for users.
  • ’s long blacklist works but becomes hard to maintain; prefer the file/directory checks above.

Practical checks: confirm mod_rewrite is enabled and AllowOverride permits .htaccess; clear browser cache when testing (301s can be cached); inspect server error logs if rules don’t take effect.

Recommended Answers

All 2 Replies

Hi,

Never tested, but it is more likely something like this will work for you.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php 

or can be something like this.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php 


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

the above should be able to handle

www.abc.com/index.php
www.abc.com/index/

RewriteEngine on



RewriteCond $1 !^(index\.php|get_captcha\.php|info\.php|blog|pagination|video|audio|uploads|images|flowplayere|mysqladmin|admindata|theme|fonts|css|cv|csv|swf|ckeditor|ckfinder|images|uploads|js|fancybox|phpreports-master|robots\.txt|favicon\.ico)



RewriteRule ^(.*)$ /root folder here/index.php/$1 [L]


//simply use your root folder  
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.