4

I have been working on getting friendly URLs to work.

On my site I have something like this:

http://www.example.com/blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant

I would like it to look something like this:

http://www.example.com/blog/Reasons+to+Use+a+Small+Business+Consultant

I have tried to edit both the httpd.conf and the .htaccess and get no response.

httpd.conf:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/sites/example
    ServerName www.example.com
    <Directory /var/www/sites/example/blog>
        Options FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

.htaccess:

RewriteEngine On
RewriteRule ^blog/Reasons+to+Use+a+Small+Business+Consultant$ blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant

LogLevel alert rewrite:trace3

The trace log says it is apply the pattern however I still see:

http://www.example.com/blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant

[Thu Oct 25 18:39:17.821619 2018] [rewrite:trace3] [pid 27933] mod_rewrite.c(470): [client 123.321.64.56:58790] 123.321.64.56 - - [www.example.com/sid#558610b6a4a8][rid#558610d3d740/initial] [perdir /var/www/sites/example/blog/] strip per-dir prefix: /var/www/sites/example/blog/newpost.php -> newpost.php, referer: http://www.example.com/blog/
[Thu Oct 25 18:39:17.821648 2018] [rewrite:trace3] [pid 27933] mod_rewrite.c(470): [client 123.321.64.56:58790] 123.321.64.56 - - [www.example.com/sid#558610b6a4a8][rid#558610d3d740/initial] [perdir /var/www/sites/example/blog/] applying pattern '^blog/Reasons+to+Use+a+Small+Business+Consultant$' to uri 'newpost.php', referer: http://www.example.com/blog/
[Thu Oct 25 18:39:17.821654 2018] [rewrite:trace1] [pid 27933] mod_rewrite.c(470): [client 123.321.64.56:58790] 123.321.64.56 - - [www.example.com/sid#558610b6a4a8][rid#558610d3d740/initial] [perdir /var/www/sites/example/blog/] pass through /var/www/sites/example/blog/newpost.php, referer: http://www.example.com/blog/
Donna Delour
  • 424
  • 5
  • 10
  • The rewrite seems to be working properly, according to your log. What is the problem you are having? – Michael Hampton Oct 25 '18 at 19:01
  • @MichaelHampton The problem is even though it says the rewrites are working I am still seeing blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant and what I would to see is blog/Reasons+to+Use+a+Small+Business+Consultant – Donna Delour Oct 25 '18 at 19:07
  • I think your usage of RewriteRule is backwards. It should be pattern to match, then the substitution. See [Apache 2.4 Docs: mod_rewrite/RewriteRule](https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule) –  Oct 25 '18 at 19:11
  • @yoonix I tried that was as well, when using `RewriteRule ^blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant$ blog/Reasons+to+Use+a+Small+Business+Consultant [NC]` the logs do show applying pattern `^blog/newpost.php?id=Reasons+to+Use+a+Small+Business+Consultant$` which is not the pattern I want. – Donna Delour Oct 25 '18 at 19:21
  • That's exactly what you want. From the URL I linked above (under RewriteRule Basics) it says: `Pattern: which incoming URLs should be affected by the rule;`. Substitution is where the requests will be sent to after the rewrite. It has to 'apply' the pattern to determine if the rewrite needs to happen. –  Oct 25 '18 at 19:33
  • Rewrites don't modify the HTML of your website. You have to do this yourself. – Gerald Schneider Oct 26 '18 at 05:47
  • @GeraldSchneider I am not looking to modify the HTML, what part of the question is misunderstood, I am obviously not explaining my question right. If I visit `http://example.com/about.htm` and want the URL to read/show `http://example.com/about` how could I do that? What does HTML have to do with changing the URL? – Donna Delour Oct 26 '18 at 13:15

1 Answers1

0

This will probably work:

RewriteEngine On
RewriteRule /blog/(.*) %{HTTP_HOST}/blog/newpost.php?$1 [L]

Your regex starts with ^blog.... This will never match what you are sending it, as the initial / is missing. The regular expression you have says match a URL starting with blog/..., but in reality there is always a slash after the hostname:port.

Refer to the documentation for RewriteRule: specifically:

The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").

My example is more generic; if you need it to be for a very specific path, as you have, then by all means change it back and replace the (.*) grouping and $1 with what you had.

ColtonCat
  • 738
  • 3
  • 7