0

I have changed the URL using the .htaccess file. When go to that URL it gives Error 404 - Not Found

.htaccess code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Product/Details/([^/]*)/(.*)/?$ index.php?u=$1&p=$2 [QSA,L]

Change aaa.com/Product/Details/index.php?u=aa&p=bb to aaa.com/Product/Details/aa/bb.

How to avoid ERROR 404 and get URL data by $_GET['u'] and $_GET['p'] or using other method.

Indra
  • 39
  • 5
  • Did you include [`RewriteEngine On`](https://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteEngine)? – showdev Apr 03 '22 at 04:14
  • Yes It is On. .htaccess file location is the main folder ('/'). – Indra Apr 03 '22 at 04:17
  • Your htaccess [seems to work](https://htaccess.madewithlove.com?share=3a02c162-781d-4cf8-9d16-820c57bd9997). Is it possible that the `index.php` file doesn't exist in that location or that some other configuration contributes to the problem? – showdev Apr 03 '22 at 04:20
  • when go to /Product/Details/ folder it will load index.php file – Indra Apr 03 '22 at 04:21
  • I think that the test code is wrong. Check this https://htaccess.madewithlove.com?share=be81c861-c35f-40d8-8471-9294de91bda0 – Indra Apr 03 '22 at 04:26
  • I need to apply code to aaa.com/Product/Details/index.php?u=aa&p=bb URL to become aaa.com/Product/Details/aa/bb URL – Indra Apr 03 '22 at 04:27
  • Oh sorry, I misunderstood. If you want to redirect *and* rewrite, you might find these helpful: [How to rewrite to a script and also redirect away from that script using .htaccess while avoiding infinite loops](https://stackoverflow.com/questions/15340785/how-to-rewrite-to-a-script-and-also-redirect-away-from-that-script-using-htacce) and [Apache .htaccess URL Rewriting](https://stackoverflow.com/questions/15243892/apache-htaccess-url-rewriting/15244599#15244599) and [Redirect to 'pretty-urls' doesn't work](https://stackoverflow.com/questions/28624342/redirect-to-pretty-urls-doesnt-work) – showdev Apr 03 '22 at 04:39

1 Answers1

0

This probably is what you are looking for:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^u=([^&]+)&p=([^&]+)$
RewriteRule ^/?Product/Details/index\.php/?$ /Product/Details/%1/%2 [R=301,L,QSD]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?Product/Details/([^/]+)/([^/]+)/?$ /index.php?u=$1&p=$2 [QSA,L]

It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues.

arkascha
  • 41,620
  • 7
  • 58
  • 90