Link misdirecting with htacess

Asked

Viewed 47 times

1

I am using the following structure in addresses/links to url friendly in fixed of my page:

RewriteRule ^teste(.*)$ teste.php  

So, my address is as follows:: www.site.com/teste
It turns out that if the user type the test word together with another term, the site finds the same way, and should send to the error page 404..
Ex: www.site.com/teste123
With the link above typed, it redirects to the test page normally..

  • 1

    You want the regex to get it only if it goes teste/ is that it? in this case test ^teste\/(.*)$

  • Rewriterule dicionario/(.*)$ dicionario.php <- So was my url. However, now the server does not find anything and sends to error 404..

1 answer

2


If you just want it to work teste, can’t have the .*.

RewriteRule ^teste$ teste.php

If you want the URL to accept more things, separated by bars, you can use it like this:

RewriteRule ^teste(/.*)*$ teste.php
  • Perfect answer, thank you. I have another question, another more complex rule. Would you rather I edit the question so you can see better or can I add it here in the same comments? The question is with the following line: Rewriterule test-([a-Z0-9-]+)/? $ test.php? Slug=$1]

  • That would be the case with a separate question, because it changes the problem. In fact, I think that there already have answer on the site, search by friendly URL, or friendly URLS. But I think it pays to separate Slug with normal bar, and not with hyphen, so you can separate the Slug/ID script from the post..

  • Is that in case I would like to show the category, and it works perfectly with /, the only problem is that when I use, my css disfigures, mainly with images..

  • This I answered these days. The problem is the css URL, if you start by slash when calling, resolve. Call your CSS like this: src="/css/meuscript.css" and not src="css/meusscripts.css". If you set the bar at the beginning, you can call qq page of the site that works.

  • I’m sorry. To tell you the truth what is not configuring is css, are the images, they are not found, because the bar is making it look like the file is in another directory, and as the link is relative the problem occurs. the link looks like this: http://localhost/site/article/test title

  • Thiago, same thing. Do not do relative to not give a headache. Or if you need to be relative, just add one .. / before. But if you can by absolute, it makes your life easier.

  • All right, excuse my ignorance. A question: if I use all the links of my site absolutely, when I am on the remote server will it delay the loading of the page? as if it were a new URL request?

  • 1

    It does not change anything, because the request is always absolute. In fact, if you do relative, who converts to absolute is the browser itself. An HTTP request always has the GET/path/absolute format. The rest is always processed by the browser to turn absolute. Do not worry about ignorance. Who asks, is "ignorant" only on time and learn (just like everyone before learning). Who doesn’t ask is who runs the risk of being ignorant forever ;)

  • 1

    This is a personal thing, but I usually create a /Assets/ folder at the root, and put everything from the site layout (images, logos, css) in subfolders (it can be a CDN too). What is content image (materials, etc.) does not go on the Assets, goes on /images/ and so on. There it is easier to organize. But it depends on the project. The important thing is the team that is developing create a rule, and everyone follow.

  • Got it! The idea is quite interesting, maybe I’ll implement it in future projects. Another thing, now that I have changed the relative address of the image to absolute, I have opened the localhost/site/article/test title link, and when trying to navigate another link, for example the contact page, was localhost/site/article/contact.. Is this because the addresses are relative? I did a test and changed the regex "agenda" to: "localhost/site/agenda" and it worked, the others got /article/location"

Show 5 more comments

Browser other questions tagged

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