Hi friends

i am facing some url redirection issue in my .htaccess. please help me to fix it.

my url is

domainname.com/article.php?url=test.html

my expected URL is

domainname.com/test.html

.htaccess

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9-/]+).html$ article.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+).html/$ article.php?url=$1

Server nginx

Dani AI

Generated

The root cause here is that you’re running nginx ( said “Server nginx”). nginx does not read Apache’s .htaccess or mod_rewrite rules, so the Apache-focused fixes suggested by , and won’t have any effect. To get the pretty URL you want you must convert the rule into nginx configuration (or implement routing in PHP if you cannot change the server config).

Example nginx approaches (place these inside the appropriate server { ... } block and test before reloading):

# map /test.html  ->  /article.php?url=test
rewrite ^/([^/]+)\.html$ /article.php?url=$1 last;
# map /test.html  ->  /article.php?url=test.html
rewrite ^/(.+\.html)$ /article.php?url=$1 last;

Or use a try_files fallback so existing files are served directly and everything else is sent to your script (note: $uri includes a leading slash; strip it in PHP or use the rewrite style above if you need a clean token):

location / {
    try_files $uri $uri/ /article.php?url=$uri;
}

Troubleshooting and gotchas: 1) Avoid rewriting requests for article.php itself (handle location = /article.php first or add a condition) so you don’t create a loop. 2) Run nginx -t to validate config and reload nginx. 3) Check nginx error and access logs to see how requests are being matched. 4) If you’re on shared hosting without nginx access, implement a small front controller in PHP that reads $_SERVER['REQUEST_URI'] and dispatches to article.php (that’s the only viable fallback when .htaccess isn’t supported).

Recommended Answers

All 10 Replies

I believe you are missing your leading slash..
RewriteRule ^/([a-zA-Z0-9-/]+).html$ /article.php?url=$1

Also remember, do MOST specific to LEAST specific. So, your order is incorrect.

You also seem to be missing some flags that will terminate the rewrite, and you possibly have yourself stuck in a loop so apache ignores the rule.

For more info, please see:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags

I do recall that a few companies (godaddy) also require a few XML type markups to your .htaccess file to work as expected. You may want to look into your hosts idea of a "properly formatted" .htaccess file for mod_rewrite.

Hi ryantroop

Thanks for your reply. i tried what you suggested but its not working.

try using

RewriteRule ^(.*).html$ article.php?url=$1

for testing only and see if its working
and remove double rules for same thing.

thanks OsaMasw.

Not working

Member Avatar for Member #120589
RewriteEngine On
RewriteRule ^([^/]*)$ /article.php?url=$1 [L]

diafol,

I am getting 404 error.

i am getting the URL as domainname/article.php?url=article.php

Hi diafol

I updated as per the link. Not working.

Member Avatar for Member #120589

Sorry can't help. Anybody else?

try:

Options +FollowSymLinks -Indexes -Multiviews

<IfModule mod_rewrite.c>

    SetEnv HTTP_MOD_REWRITE On
    RewriteEngine on
    RewriteBase /

    RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|css|js)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*).html$ /article.php?url=$1 [NC,L]

</IfModule>

basically
set a few options (including the all important +FollowSymLinks)

if mod_rewrite exists
Enable it
set the base to /
make sure we dont ever rewrite direct requests for images, css, or js files
or any files
or a proper directory
and then rewite our url pattern.

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.