简体   繁体   中英

Can't resolve htaccess rewrite issue (seo-friendly URL)

Hi. First of all I am very new to Stack Overflow so excuse me if I did something wrong.

I need some help on the .htaccess to rewrite a rule for a SEO friendly URL. Basically it is a blog and I get blog post data not with an $id but with a sef URL parameter. I know that search engines does not like URL parameters because it is hard to identify what is what.

"sef" parameter dynamically changes according to the blog post content I show. I want to get the part after "=" and delete parameter name(?sef) and put a "/" in between which looks like this.

Current URL structure (with a "sef" parameter)

https://www.dent.com.tr/gelismeler.php?sef=dis-tedavisi-nasil-genclestirir

I want to rewrite this url as

https://www.dent.com.tr/gelismeler/dis-tedavisi-nasil-genclestirir

I check/set page content data dynamically from a mysql database within gelismeler.php

if (isset($_GET['sef'])) {
$sef = $_GET['sef'];

$sql = "SELECT * FROM posts WHERE sef='$sef'";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($query);

$title = $row["title"];
$desc = $row["description"];
$img = $row["image"];

I've tried countless variations but actually I've never succeed:(

Can anybody help me with this?

Thanks a lot!

My current.htacces file:

ErrorDocument 404 /404.php
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php

RewriteCond %{HTTP_HOST} !^www.dent.com.tr$
RewriteRule ^(.*)$ http://www.dent.com.tr/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(php|html) [NC]
RewriteRule ^index\.php$ http://www.dent.com.tr/ [R=301,L]

Have it this way:

ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /

# add www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# remove index.php or index.html
RewriteCond %{THE_REQUEST} \s/+index\. [NC]
RewriteRule ^index\.(html?|php)$ / [R=301,L,NC]

# external redirect from actual URL to pretty one
# 2 parameter redirect rule
RewriteCond %{THE_REQUEST} \s/+gelismeler\.php\?category=([^&]+)&sef=([^\s&]+)\s [NC]
RewriteRule ^ /gelismeler/%1/%2? [R=301,L,NE]

# 1 parameter redirect rule
RewriteCond %{THE_REQUEST} \s/+gelismeler\.php\?sef=([^\s&]+)\s [NC]
RewriteRule ^ /gelismeler/%1? [R=301,L,NE]

# internal forward from pretty URL to actual one
# 2 parameters rewrite
RewriteRule ^gelismeler/([\w-]+)/([\w-]+)/?$ gelismeler.php?category=$1&sef=$2 [L,QSA,NC]

# 1 parameter rewrite
RewriteRule ^gelismeler/([\w-]+)/?$ gelismeler.php?sef=$1 [L,QSA,NC]

# add .php internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

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