About URL rewrite

Hello,
I’m not sure if the topic is the right place, if it’s not the right place, please move it

I create menu name and url by pulling in database as below

while ($row = $menus->fetch()){
 echo "<li><a href=\"index.php?p=".$row['url']."\">".$row['menu_name']."</a></li>\n";
}

I am using it to show page content with GET parameter in index.php page

How to rewrite url with .htaccess or PHP
I searched a lot but couldn’t find anything.

https://domain.com/index.php?p=examples
https://domain.com/index.php?p=about
https://domain.com/index.php?p=satellite_antenna_system

I want it to look like below in browsers and work when this url is entered

https://domain.com/examples
https://domain.com/about
https://domain.com/satellite_antenna_system

I want to create the most useful URL for SEO

Thank you

Adem, SEO is a hard thing to fix. Here is a link that explains some common practices on how to handle your URL’s. It might give you ideas for the future. SEO-Best-Practices

As far as your example, you would just do this in your .htaccess file. Here is a simple tutorial on all of these rewrite processes. The part you want is almost 1/2 way down labeled Example#2. It shows how to remove the ?= section and turn it into a folder type of name. You will have to play with it and adjust to fit your needs. Should explain it and help you.

By the way when searching for this on the net, don’t look for SEO, but, look for " mod rewrite " or " mod_rewrite " which is the term for this type of code. Good luck!

Thank you,
My website will have 5-6 pages and I display them as index.php?p=page1
So I don’t need anything too advanced

SEO is an advanced coding and I know it’s hard
I just wanted to say what is the most suitable url for search engines
Sample:
com/this_is_a_page “underscore ?”
com/this-is-a-page “n-rule ?”
com/this is a page “space between words?”
and also is it more convenient to have html in the extension?
Note: There is no need to write code in .htaccess for the page name, I specify the page name from the admin panel.

I create 6 pages as index.php?p=page

I found the following code on the internet

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Remove index.php
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !^/index\.php/admin\b [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Remove ?p=
    RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)p=([^&\s]+)&?([^\s]*)
    RewriteRule ^(.*)$ /%3?%1%4 [L,R=301]


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

This .htaccess code generated the URLs the way I wanted
com/index.php?p=page ===> com/page

I want to add a feature to this .htaccess code
I have two pages “com/login.php” and “com/register.php”
I want to remove php extensions
There are codes that remove the php extension but I couldn’t include it in the above code

Thank you

As per the first link I posted, it says no spaces. ( I knew that already ) And, dashes are better than underscores.

Removing PHP is always good. As for the ?p=page, you can remove everything starting at the ?..
The rewrite code can de that. For all six pages p=1 to 6, it would just show index that way.
Glad you figured it all out.

Something like this will remove the PHP from the ones that have it:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

And, this version will remove HTML extensions, too:

RewriteRule ^([^\.]+)$ $1.html [NC,L]

Existing code just adding one line as shown above but it didn’t work how should I do

It also prevented me from accessing the admin panel
/adminpanel/index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

This is not the same as this:

RewriteRule (.+) index.php?p=$1 [QSA,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L] // I just added this here but it didn't work. how should i do

The first rule messes up the second rule. Try using the first version… Like this:

# Remove the .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

@ErnieAlex I’m sorry, the code that is blocking my access to the admin panel is not your code, my code is blocking my access to the admin panel

interesting situation
There is no problem to login to the administration panel on the local server, but it blocks it on the remote server.

I need the code from the beginning but I don’t understand it at all
My URLs are like this
com
com/login.php
com/register.php
com/reset.php
com/index.php?p=xxxx-xxxx
com/adminpanel/index.php

I will be glad if you help

Well, I got some time to look at your rewrite code. I copied some of it in my examples from a website just for you to have an example. It seems like there are some errors in it. I will test it on my htaccess testing site and see what the errors are. This will take a little time. Please check back in about an hour or two…

Okay, step by step. The following works. I tested it with this URL:

http://www.ernie.com/index.php?p=page

The code I used is:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*)&?p=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L]

The resulting URL gives this results:

http://www.ernie.com/index.php

Try this first step and see it if removes the parm “p” from your URLs.

( We do not care about the ADMIN panel URL’s as only the admin would ever see it. )

Another way is to make a URL from this:
www.example.com/index.php?p=page to www.example.com/page

RewriteCond %{THE_REQUEST} /(?:index\.php)?\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

That might be handy for you, too.

gave 404 error

The following code allowed to access the admin panel
RewriteCond %{REQUEST_URI} !^/?(adminpanel)/

As I said before, Only you as the admin will see the URL for that. Why bother to rewrite it.
Also, that (adminpanel) is NOT a rewrite command! Use braces.

I am typing the admin panel url directly but it was redirecting to the home page
I found the following code on the net, I added it, the problem was fixed
RewriteCond %{REQUEST_URI} !^/?(adminpanel)/

I found the following code on the net. Removed the .php extension
I added the code except ".com/adminpanel/index.php " because it also removed the index.php extension in the admin panel

# Remove .php:
RewriteCond %{REQUEST_URI} !^(index.php)
RewriteCond %{REQUEST_URI} !^/?(adminpanel)/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NC]

I don’t know anything about .htaccess, I’m trying to do something with what I found on the net

If you would like to learn, here is a random htaccess tutorial.
https://www.askapache.com/htaccess/

Here is one of many htaccess syntax checkers
https://htaccess.madewithlove.be/

I can’t solve the problem anyway,
Redirects to home page even though I enter a direct URL
It prevents me from accessing the admin panel

login.php
reset.php
register.php
I remove the extension of 3 files as above with the code at the bottom
(other php files exist but they will remain)

For the pages in the menus on the homepage, I pull the page contents from the database as “index.php?p=page-page”

The following code converts the url “index.php?p=page-page” to “com/page-page”

That’s all, how do I get access to the admin panel?
What are the errors in the codes below for me

I ask for your help
Thank you

<IfModule mod_rewrite.c>
    Options +MultiViews

    RewriteEngine On

    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://xxxxxxxx.com/$1 [R,L]

    # Except for the administration panel folder
    RewriteCond %{REQUEST_URI} !^/administrator/
	
	# Remove index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Remove ?p=
    RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)p=([^&\s]+)&?([^\s]*)
    RewriteRule ^(.*)$ /%3?%1%4 [L,R=301]

    # Craft 404
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]

    # Remove extension of specified files
    RewriteCond %{THE_REQUEST} \s/+(login|register|reset)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=302,L,NE]

</IfModule>

I am using this code in index.php on homepage

if(isset($_GET['p'])){
    $get_url_name = filter_input(INPUT_GET,'p');
    $page = $pdo->prepare("SELECT get_url_name, homepage FROM front_pages WHERE get_url_name=?");
    $page->execute([$get_url_name]);
    $read_content = $page->fetch();
    $homepage = $read_content['homepage'];
}else{
    $page = $pdo->prepare("SELECT get_url_name, homepage FROM front_pages WHERE homepage=?");
    $page->execute([1]);
    $read_content = $page->fetch();
    $homepage = $read_content['homepage'];
    $get_url_name = $read_content['get_url_name'];
}

I think when you combine different rules, you must use the “rewrite inherent” option. Otherwise the last rule takes over. You can read how to program htaccess here: Rewrite-Rules
As Benanament posted, you can check your syntax online. It is easy. Just enter your URL with all the extras on it and enter your rewrite code and test it. Fix the errors until it works.

There may be an error in my Rewrite-Rules code because I combined what I found here and there. I don’t know about this

I found the problem
I added the following code in index.php and saw the error 401.shtml in the results text file
I saw this site when I searched for 401.shtml error: htaccess redirect to https may cause redirect to 401.shtml when using password protection - Really Simple SSL
my admin panel directory is encrypted, the problem was caused by this
I added the code given on the site to the .htaccess file in the admin directory and the problem was fixed. :grinning:

	$file = fopen("student.txt","a");
fwrite($file, "\n". $get_url_name);
fclose($file);

I am so happy :grinning:
Thank you

Congratulations! Always nice to solve a programming puzzle. You did not tell us your ADMIN page was encrypted. Good luck with the rest of the site !

I didn’t even think that this problem would be caused by the directory password.

Thank you, no need for now

Sponsor our Newsletter | Privacy Policy | Terms of Service