User-friendly URL with more than one parameter with . htaccess php

Asked

Viewed 97 times

0

I am having problems with my URL friendly, I have a code that already works, however for a specific directory.

.htaccess

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$  index.php?url=$1

index php.

$url = (isset($_GET['url'])) ? $_GET['url'] : 'mdjr_home';

$arq_perm = array('mdjr_home','mdjr_contatos','mdjr_orcamento',
    'mdjr_portifolio','mdjr_quem_somos','mdjr_videos','mdjr_teste');

$pasta = "producoes/page_mdjr/pages_menu";

if (substr_count($url,'/') == 0)
{
    $url = explode('/',$url);
    $page = (file_exists("{$pasta}/".$url[0].'.php') && in_array($url[0],$arq_perm)) ? $url[0] : "404";
    $id = intval(isset($url[1]));

}elseif(substr_count($url,'/') == 0)
{
    $page = (file_exists("{$pasta}/".$url.'.php') && in_array($url[1],$arq_perm)) ? $url : "404";
    $id = 0;

    // echo $page;
}

require ("{$pasta}/{$page}.php");

Just added it to my menu. php and my friendly menu url works,, but in my home which is the main page I have some cards it generates me these urls:

mdjr/producoes/controller/controller.php?card=0
mdjr/producoes/controller/controller.php?card=1
mdjr/producoes/controller/controller.php?card=2
mdjr/producoes/controller/controller.php?card=3
mdjr/producoes/controller/controller.php?card=4

because by clicking it opens the link of the page

mdjr/producoes/controller/controller.php

where I say what will be displayed on each thing, but I don’t know what to do for both the php code in my index.php and my . htaccess works until I included some of those codes in the . htaccess but it didn’t work out. I tried to add :

RewriteRule ^(.*)\/?(.*)\/?$ index.php?url=$1&card=$2
RewriteRule ^producoes/controller/([a-z0-9-]+)/([0-9]+)/?$/controller.php?url=$2&card=$1 [NC]

RewriteRule ^producoes/controller/.*?$ controller.php?card=$1

Along with the existing but nothing works, always returns the error page, without it the page is opened normally and works, but when adding these codes give error.

  • What exactly is the URL template you need. Post an example of how you would like the URL to be

  • Is this the full address http://localhost/mdjr/producoes/controller/controller.php? card=0 would like it to be after the/mdjr/controller domain only that, regardless of the id I clicked, or it could be with the after without problems, but I don’t know if it all depends solely on php or somnete’s programming. htaccess

1 answer

0

Based on your comment, you would like to mount the url in the following structure: dominio/mdjr/controller/(id ou não), correct? And this url would point to controller.php.

Could use this on .htaccess:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^mdjr/controller\/?$ controller.php?card=0 [NC,QSA]
RewriteRule ^mdjr/controller/([A-Za-z0-9-]+)\/?$ controller.php?card=$1 [NC,QSA]

php controller.

$card = $_GET['card']

However, I saw that in your code there is an array with names of something. You will inform me better to update the answer if it is not only this.

  • I changed my . htaccess added the code above Rewriteengine On Rewritecond %{SCRIPT_FILENAME} ! -f Rewritecond %{SCRIPT_FILENAME} ! -d Rewriterule (. *)? index.php? url=$1 Rewriterule mdjr/controller/? $ controller.php? card=0 [NC,QSA] Rewriterule mdjr/controller/([A-Za-Z0-9-]+)/? $ controller.php? card=$1 [NC,QSA e ficou assim, porém a agora da erro, sim, no meu index.php tenho um array $arq_perm = array('mdjr_home','mdjr_contatos','mdjr_orcamento',
 'mdjr_portifolio','mdjr_quem_somos','mdjr_videos','mdjr_teste');

  • And it takes the name of my menu files for example that is in producoes/page_mdjr/pages_menu/mdjr_home it leaves the url of my menu friendly, but as my controller.php is two levels up, I think my php code does not suit it! I actually took this code from the Internet and modified it for myself.

  • Not ideal to use (.*) htaccess. This may be the reason for your mistake. You have tested only with the rules I put in?

  • I made the changes I put only the rules that you put the main works, but by clicking on the error menu, page not found, with if the php code did not work, as I know that in the code it Busta by the url parameter to my menu pages, then I changed everything as a card to url but memo so nothing, because maybe better to set parametr url to the menu and card to the links of the card

  • If I leave this rule the friendly url of meni back to smoke, how would this rule without an asterisk? Rewriterule (.*)? index.php? url=$1 the controller link url still remains http://localhost/mdjr/producoes/controller/controller.php? card=0 and I wanted it to be http://domain/mdjr/controller/ with or without optional id remembering that my menu files are in another directory where the php code

  • will fetch them in this path $folder = "producoes/page_mdjr/pages_menu"; and the controller.php finds itself in producoes/controller/controller.php call by clicking this link <a href=".. /mdjr/producoes/controller/controller.php? card=<?= $Count++ ? >" target="_Blank"> which is in my home

  • I’m still trying to understand your logic. You want it to stay that way: dominio/mdjr/controller/(id ou não). But how do you expect to know which array page to include? You would need one more parameter, for example: dominio/mdjr/controller/pagina_menu/(id ou não). Would that make sense? Both will be dealt with in index.php, correct?

  • Yeah, maybe I did think wrong, is like my menu.php this several Nives to low type mdjr/producoes/page1/page1/home.php and with friendly url got so domain/mdjr/home thought that if I just use a controller.php file to print for me it would be a good since it would not be any menu not want to create another one page in the menu, and with this more levels above it would be easier for example this and mdjr eem (Obs is the project folder) mdjr/controller/controller.php thought we could leave something like mdjr/controller

  • I will change this name later to not appear like this in the url, it is okay if it is mdjr/controller/controller I will change this name to look better

  • No problem, we are adjusting to improve the project. Look, with friendly URL, you can even eliminate mdjr and controller of the url, for example: dominio.com/seu_menu/(id ou não). Since you would like to treat everything in controller.php. It would be quiet to do so. Let me know if this is how you would like then update the code.

  • Since I’m not so experienced what I did, I left the index.php file with the code for a friendly url, and I did this in my menu.php <?php require_once DIR . '/../../../index.php';? > as in my home have some cards co some links that is generated dynamically with a sequence of number I created the folder and file controller so to try to display the images that I caught using php’s Interator ai as the link that click with card=0 or card=1 parameter it displays what I want inside the controller with some if’s,which most correct?

  • appeared message saying it has many comments, if you think it best, by me it is okay, but and prefer to help me in the chat without problems too!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • We can chat yes around 2:00

  • I can’t move haha but I’m still waiting for your help, to help me understand what I’m missing

  • https://chat.stackoverflow.com/rooms/191231/dm707-joannes

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.