I am having a hard time rewriting my URL in htaccess to be more SEO friendly - apache

My current .htaccess file looks like this:
RewriteEngine on
RewriteRule ^pages/([\w-]+)/artwork/?$ painting.php?painting=$1 [QSA,NC,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
I am trying to make the URL instead to look like example.com/artwork/paintingURL and I am getting to this page from <a href = "painting?painting=<?=$paintingURL?>">. I've searched here and of the solutions I've gotten, none of the answers seem to work for me.
Update: After using RavinderSingh13's solution below, I am now getting 404 page not found error on the redirected url. On painting.php :
<?php
if(isset($_GET['painting'])) {
include('include/conn.php');
$paintingURL = $_GET['painting'];
$sql = "SELECT * FROM `paintings` WHERE `painting_URL` = '$paintingURL'";
$result = $conn->query($sql);
global $paintingName;
global $paintingDesc;
global $paintingImg;
global $pageTitle;
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)){
$paintingName = $row['painting_Name'];
$pageTitle = "Site Name - ".$paintingName;
$paintingDesc = $row['size_description'];
$paintingImg = $row['image_ID'];
}
} ?>
jsfiddle for how links are served: https://jsfiddle.net/h65u9fc3/

With your shown samples, attempts please try following .htaccess rules file.
Please make sure to:
Keep your .htaccess rules file along with your project-name folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /project-name/
RewriteCond %{THE_REQUEST} \s/(project-name)/painting\?painting=(\S+)\s [NC]
RewriteRule ^ /%1/artwork/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(project-name/artwork/)([^/]*)/?$ project-name/painting.php?painting=$1 [QSA,NC,L]

Related

htaccess remove .html from url not working

hi i am trying to remove .html from url.
example : from: example.com/home.html to example.com/home
i ran into some videos and posts regarding .htaccess file.
i used this code for htaccess and checked it on htaccesschecker , it says syntax is ok.
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
i am trying to do it first on vscode. when i run live server i just get same .html ending for every html file...
With your shown samples please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect rules here.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.html\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.html [L]

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]

.htaccess remove file extension with exception

I want to remove all the file extensions (like .php) from my URL/website and redirect to the pages without the extensions. I came up with this solution so far (in .htaccess):
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
This works fine so far.
But I need an exception rule for one site. This site should keep his file extension or else I can't send data via the POST method. The solution most users offer is this:
RewriteEngine on
RewriteRule ^todo\.php$ - [L]
But this solution doesn't work for me for whatever reason. Does anyone else has an idea?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Try this

Using RewriteRule to write URL from page/unique_id to page.php?id=unique_id

I am trying to format my URL from http://jacqueskoekemoer.co.za/article/5 to http://jacqueskoekemoer.co.za/article?i=5 where i would be the article ID in the database.
Currently this URL works. But this one does not. It does change the url to article.php but it doesn't add the query string to the end.
I know this because it does not appear in print_r($_REQUEST); or print_r($_GET);
What am I doing wrong in my .htaccess file, or can you suggest other resources that I could look at to resolve the problem?
I have used the following 2 URL's primarily to get the RewriteRule working.
URL 1
URL 2
In .htaccess file the rule rewrite section looks like this:
RewriteEngine on
RewriteRule ^article/$ article.php?i=$1 [L] # Handle requests for "article"
#RewriteRule ^article/$/ article.php?i=$1 [NC,L] # Handle requests for "article"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Use the following rule:
RewriteEngine on
RewriteRule ^article/(\d+)$ article.php?i=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

htaccess RevriteEnngine url doesnt rewrite Why?

in the same folder where there is index.php I have .htaccess file with the following code inside:
RewriteEngine On
RewriteRule ^example.com/(.+)$ example.com/secondpage.php?firmname=$1 [R,L]
so that instead of
example.com/secondpage.php?firmname=GGGGGGG
it should show
example.com/GGGGGGG
but it doesn't work and I cant find mistake there...
You don't want the hostname in the regex and rule's target. It's also showing the URL with the query string because you are using the R flag, which redirects the browser and thus changes the address in the location bar.
You probably want something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /secondpage.php?firmname=$1 [L]
And maybe:
RewriteCond %{THE_REQUEST} \ /+secondpage\.php\?firmname=([^&\ ]+)
RewriteRule ^ /%1? [L,R]