简体   繁体   中英

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

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']

This is possible for all pages using language/content negotiation in the Apache configuration (httpd.conf or apache2.conf) along with rewrite rules. Use of an .htaccess file is not required.

In apache2.conf file (or httpd.conf) add the Multiviews directive, MultiviewsMatch directive, and these rewrite rules :

<Directory /var/www/html>
    Options Indexes FollowSymLinks Multiviews
    AllowOverride None
    Require all granted

    RewriteEngine On
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [NC,L,R]

    <Files "*.php">
        MultiviewsMatch Any
    </Files>
</Directory>

Then save the file and restart Apache sudo systemctl restart apache2

When you declare further directives in apache2.conf, repeat the Multiviews directive for each:

<Directory /var/www/html/example.com/>
  Options FollowSymLinks Multiviews
  AllowOverride All
  Require all granted
</Directory>

Make sure you enable/un-comment the rewrite and negotiation modules in apache2.conf:

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule negotiation_module mod_negotiation.so

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM