3 Replies - 367 Views - Last Post: 26 January 2019 - 08:33 PM

#1 O'Niel   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 469
  • Joined: 13-September 15

Htaccess rewrite for SEO-friendly URLs

Posted 25 January 2019 - 11:12 AM

Hi!

I'm trying to rewrite blog.php?article=My-title to blog/article/My-title.
This is my Htaccess file:

#RewriteEngine On

#DirectoryIndex home.php
#Redirect /index /home

RewriteBase /
RewriteRule ^blog/article/([0-9A-Za-z-]*)/?$ blog?article=$1 [NC,QSA,L]

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

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

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:ndvibes.com
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^ndvibes.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:ndvibes.com




When visiting website.com/blog/My-title now, I get a 404 File not Found.
How to fix this? Thanks!

This post has been edited by O'Niel: 25 January 2019 - 11:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Htaccess rewrite for SEO-friendly URLs

#2 O'Niel   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 469
  • Joined: 13-September 15

Re: Htaccess rewrite for SEO-friendly URLs

Posted 25 January 2019 - 11:40 AM

Fixed it!
#RewriteEngine On

#DirectoryIndex home.php
#Redirect /index /home

RewriteBase /
RewriteRule ^blog/([a-zA-Z0-9-]*)/?$ blog.php?article=$1 [NC,QSA,L]

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

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

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:ndvibes.com
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^ndvibes.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:ndvibes.com




Accessable at blog/My-title
Was This Post Helpful? 0
  • +
  • -

#3 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Htaccess rewrite for SEO-friendly URLs

Posted 25 January 2019 - 07:01 PM

Care to share how you resolved it?

You can refer to this working and tested version here if you need an example to follow :: https://www.dreaminc...ost__p__2388458
Was This Post Helpful? 0
  • +
  • -

#4 O'Niel   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 469
  • Joined: 13-September 15

Re: Htaccess rewrite for SEO-friendly URLs

Posted 26 January 2019 - 08:33 PM

View PostSheepings, on 25 January 2019 - 07:01 PM, said:

Care to share how you resolved it?

You can refer to this working and tested version here if you need an example to follow :: https://www.dreaminc...ost__p__2388458


The problem I was encountering, was that in my htaccess I'm also removing php-extensions, so I was doubting if I should rewrite just 'blog' or 'blog.php'.

Correct code:
RewriteBase /
RewriteRule ^blog/([a-zA-Z0-9-]*)/?$ blog.php?article=$1 [NC,QSA,L]


Wrong code:
RewriteBase /
RewriteRule ^blog/article/([0-9A-Za-z-]*)/?$ blog?article=$1 [NC,QSA,L]


In the correct code I rewrite it for blog.php, not just blog.
Also the place in the htaccess matters, placing this code at the bottom of the htaccess (after php-extension rewriting) wont work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1