Home > Software design >  Change the url type in PHP
Change the url type in PHP

Time:09-26

The links on my site are displayed as follows:

blog.php?id=Blizzard

How can I convert them as follows?

blog/Blizzard

Using .htaccess or PHP

please guide me


I used the following phrase:

RewriteRule ^blog/(.*)$ ./blog.php?id=$1

But the images , css and javascript codes are destroyed

CodePudding user response:

You can use .htaccess to create short urls.

examples:

RewriteEngine On
RewriteBase /
RewirteRule sample /sample2.php

just 3 line need

RewriteEngine On <== Use Rewrite Module
RewriteBase / <==  .htaccess file locate
RewriteRule  <== ShortURL RULE

you write

RewirteRule sample /sample2.php

use http://127.0.0.1/sample

redirect sample2.php

CodePudding user response:

But the images , css and javascript codes are destroyed

Probably because you are using relative URLs to your static resources. If you are referencing an image as img/myimage.jpg then the browser is now going to look for /blog/img/myimage.jpg, not /img/myimage.jpg as before (using your example).

You need to use root-relative URLs (starting with a slash) or absolute URLs to be able to access the resource from different URL-path depths. You can sometimes workaround the issue with the base element, however, there are caveats with this approach.

See my answer to the following question on the Webmasters stack that goes into great detail on this exact issue: .htaccess rewrite URL leads to missing CSS


RewriteRule ^blog/(.*)$ ./blog.php?id=$1

You should remove the ./ prefix on the RewriteRule substitution. The dot (to indicate the "current directory") serves no purpose here in the URL-path (it gets resolved away).

You should also include the L flag, should you have other rules.

You also need to ensure that MutliViews is disabled, otherwise, blog.php will be "magically" called without the id attribute. See https://httpd.apache.org/docs/2.4/content-negotiation.html

Optionally, if you are changing a URL structure, you can also redirect URLs of the form /blog.php?id=<value> to /blog/<value> in order to preserve SEO and redirect bookmarked links. (But note that you should already be linking to URLs of the form /blog/<value> throughout.)

For example

# Disable MultiViews
Options -MuliViews

# Redirect "/blog.php?id=<value>" to "/blog/<value>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=([^&]*)
RewriteRule ^blog\.php$ /blog/%1 [QSD,R=301,L]

# Rewrite "blog/<value>" to "/blog.php?id=<value>"
RewriteRule ^blog/(.*) blog.php?id=$1 [QSA,L]

Additional notes on the above directives:

  • The check against the REDIRECT_STATUS environment variable ensures that only direct requests are redirected and not rewritten requests by the rewrite that follows.
  • The QSD flag discards the original query string from the request.
  • The QSA flag appends (merges) any query string that might be on the original request with the query string being assigned in the substitution. If you are not expecting any query string then remove this flag. eg. A request for /blog/Blizzard?foo=1 would be internally rewritten to /blog.php?id=Blizzard&foo=1 only if the QSA flag is present.
  • Related