Skip to content
Advertisement

clean url in core PHP

I’ve gone through the multiple threads on clean url,but still I get confuse on it.I want to use clean url strategy in core php website.I changed my .htaccess file for multiple urls.but my following code works only for one page(one url)

 # Turn on the Rewrite Engine
 RewriteEngine on
 # Rewrite for buyleads.php
 RewriteRule ^buyleads buyleads.php [NC,L]
 ## NC makes the rule non case sensitive
 #L makes the last rule that this specific condition match

 # Rewrite for search.php?u=xxx

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$  search.php?cat=$1 [L]



 # Rewrite for search.php?u=xxx&subcat=xxx

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([^/]+)/([^/]+)$  search.php?cat=$1&subcat=$2 [L]
 # Rewrite for detail.php?u=xxx

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^detail/([a-z0-9]+)$ /detail.php?id=$1 [L]

Question 1. Above code works only for search.php and if I entered www.sitename.com/detail/29 (detail.php code) it works like www.sitename/29 (search.php) . What mistake I have done in it

Question 2. I also want to know is it really possible to make all urls like this through only .htaccess file? Is there any way to create in core php ?

Question 3. lastly I want to know that how can auto redirect to clean url through .htaccess , like CLick this after clicking user should go www.sitename.com/34/50.

Thanks in advance

Advertisement

Answer

About your questions:

  1. Your second rewrite rule does not exclude urls that start with detail/ so your third rule will never be reached as the second one will search for category detail and sub-category 29 (in your specific example). You can exclude detail/ from your second rule but you can probably also change the order of the 2nd and 2rd rules to make it work.
  2. Yes, you can rewrite all urls and have your logic all in php. You can send the original path as the parameter and parse / analyze that in php.
  3. You should generate your links correctly to start with. That way you don’t have to redirect and you don’t have to tell the search engines that a page is permanently moved when you do redirect.

A small example for point 2:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$  /index.php?url=$1 [L]

Now all urls will be rewritten and in your script you can get the original url / path in $_GET['url']

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement