I have problem with this htaccess in STATIC URL

RewriteEngine On    
RewriteRule ^readlex/(.*)$/?$          readlex.php?slug=$1 [NC,L]

DINAMIC URL work fine only with defined CONSTANT: Example

<a href="<?php echo BASE_URL.READ_LEX ?>Le-mura-di-Lucca">Lucca</a>

Result = localhost/mysite/readlex/Le-mura-di-Lucca

STATIC URL not work (??)

<a href="localhost/mysite/readlex.php?slug=Le-mura-di-Lucca">Lucca</a>

Result = localhost/mysite/readlex.php?slug=Le-mura-di-Lucca">Lucca

***Unfortunately I can't insert the php CONSTANTS in mysql varchar/text

Recommended Answers

All 12 Replies

I suppose I’m not properly understanding your question. Do you mean when you go to the URL in your browser localhost/mysite/readlex.php?slug=Le-mura-di-Lucca it doesn’t load, but when you go to the rewritten URL it works fine?

NO! I meant that URL loads but not friendly
I have to put these STATIC URLs inside the text retrieved from the database as I cannot insert php strings.
Example from database example that htaccess makes unfriendly :

<em><a href="../read-lex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari">Vedi &nbsp;SENTENZA</a></em>

While is impossible insert in database:

<em><a href="<?php echo BASE_URL.READ_LEX ?>Le-mura-di-Lucca">Lucca</a></em>

That is, this htaccess does not work with STATIC URL, but load page not friendly as in the case of the DYNAMIC URL

I apologize that I still am misunderstanding you.

I'm confused what you mean by "example from database", because it seems your question is about htaccess and PHP. I'm confused what is being retrieved from the database

In your second example, what are the values of BASE_URL and READ_LEX?

Additionally, your .htaccess file said readlex.php but in your example in your previous post, you have read-lex.php.

You should be able to use the following .htaccess file I have below to convert:

http://localhost/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari

into:

http://localhost/readlex/provvedimento-di-sospensione-trib-cagliari

RewriteEngine On    
RewriteRule ^readlex/(.+)$ readlex.php?slug=$1 [NC,L]

I apologize because I am unclear. Difference between readlex and read_lex is my mistake

Your htaccess example is right except that it doesn't work for me and I don't understand.
Summary:

In <head> I have this php:

define("BASE_URL", "http://localhost/mysite/");

if($mod_rewrite == 'Off') {
    define("URL_CATEGORY", "category.php?slug=");
    define("URL_PAGE", "page.php?slug=");
    define("URL_NEWS", "news.php?slug=");
    define("URL_GALLERY", "gallery.php?slug=");
    define("URL_PDF", "pdf.php?slug=");
    define("URL_PHOTO", "photo.php?slug=");
    define("URL_VIDEO", "video.php?slug=");
    define("URL_YOUTUBE", "youtube.php?slug=");
    define("URL_PUBLISHER", "publisher.php?publisher=");
    define("URL_SEARCH", "search.php");
    define("READ_LEX", "/readlex.php?slug=");
    define("URL_LEX", "lex.php?slug=");

} else {
    define("URL_CATEGORY", "category/");
    define("URL_PAGE", "page/");
    define("URL_NEWS", "news/");
    define("URL_GALLERY", "gallery/");
    define("URL_PDF", "pdf/");
    define("URL_PHOTO", "photo/");
    define("URL_VIDEO", "video/");
    define("URL_YOUTUBE", "youtube/");
    define("URL_PUBLISHER", "publisher/");
    define("URL_SEARCH", "search");
    define("READ_LEX", "readlex/");
    define("URL_LEX", "lex/");
}

Which I then recall in this link and htaccess works fine:

<a href="<?php echo BASE_URL.READ_LEX ?>Provvedimento-di-sospensione-Trib-Cagliari</a>  // out of database. In database mi dà error

Result: http://localhost/mysite/readlex/provvedimento-di-sospensione-trib-cagliari OK OK

The same link without Constant PHP:

 http://localhost/mysite/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari  // inside or out database 

Result: http://localhost/mysite/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari NOT OK

In <head> I have this php:

I see you have on line 3 if($mod_rewrite == 'Off') {. Where do you set the value of $mod_rewrite? How do you tell if it's Off or On?

Which I then recall in this link and htaccess works fine:

Again, where do you set the values for BASE_URL?

The same link without Constant PHP:

What do you mean by 'Constant PHP`?

Where do you set the value of $mod_rewrite?

I set On-Off $mod_rewrite in database from Dashboard Admin.
Now is always ON.

Again, where do you set the values for BASE_URL?

In Admin config.php

// Defining base url
define("BASE_URL", "http://localhost/mysite/");

What do you mean by 'Constant PHP`?

BASE_URL    //= http://localhost/mysite/
READ_LEX    //= readlex.php?slug=

My mistake

define("READ_LEX", "readlex.php?slug=");   //  this OK

Is the value of BASE_URL set to http://localhost/mysite or http://localhost/mysite/ ?

As Above
The problem is that this htaccess only works with DYNAMIC URLs and not with STATIC URLs (??) and I have to put the URLs in the database ( But only STATIC )

I expressed it badly. They are both dynamics. But:

href="<?php echo BASE_URL.READ_LEX ?>Provvedimento-di-sospensione-Trib-Cagliari"   // result = localhost/mysite/readlex/Provvedimento-di-sospensione-Trib-Cagliari

href="localhost/mysite/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari" // result = localhost/mysite/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari

Solved the problem with this htaccess in mode rewrite ON
But I don't understand why. I have to enter friendly url directly

 href="<?php echo BASE_URL.READ_LEX ?>Provvedimento-di-sospensione-Trib-Cagliari"    
// OK result = localhost/mysite/readlex/Provvedimento-di-sospensione-Trib-Cagliari

    href="localhost/mysite/readlex/Provvedimento-di-sospensione-Trib-Cagliari"          
// OK result = localhost/mysite/readlex/Provvedimento-di-sospensione-Trib-Cagliari

    href="localhost/mysite/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari" 
// NOT OK = localhost/mysite/readlex.php?slug=Provvedimento-di-sospensione-Trib-Cagliari

Thank you for your patience

The .htaccess makes the URL accessible from a friendly URI. However, it does not change any of your HTML code for you. You will still need to modify all of your HTML code to link to the new friendly URI instead of the original URL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.