1

I have a problem with transforming urls from dynamic to static.

I have a site where different pages are generated with a dynamic url, like:

www.example.com/?pr=project-abc123

I would like to rewrite the url of each one with htaccess making it static, like this:

www.example.com/project-abc123

// or

www.example.com/pr/project-abc123

Now, i found this htaccess code that seems to work:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule (.*) http://example.com/%1? [L,R=301] 
RewriteRule ^(.*)$ /index/?pr=$1[L]

URLs are rewritten as indicated (first type, whitout /pr/ ), but gives me a multiple choice error. What am I doing wrong?

unclexo
  • 3,691
  • 2
  • 18
  • 26
spanboy
  • 23
  • 2
  • 1
    Does this answer your question? [URL rewriting with PHP](https://stackoverflow.com/questions/16388959/url-rewriting-with-php) – unclexo Feb 25 '20 at 15:42
  • 1
    See this one is well https://stackoverflow.com/a/36590510/12232340 –  Feb 25 '20 at 15:59
  • 1
    `"multiple choice" error` - ? Presumably you have already changed the URLs in your application? – MrWhite Feb 25 '20 at 16:03
  • Nothing of this. The methods in the other topics don't work. the message is: '300 Multiple choice. The document name you requested (/.php) could not be found on this server. However, we found documents with names similar to the one you requested.' – spanboy Feb 25 '20 at 17:41

1 Answers1

0

Let's understand RewriteRule directive which is the real rewriting workhorse.

A RewriteRule consists of three arguments separated by spaces. The arguments are

  • Pattern: which incoming URLs should be affected by the rule;
  • Substitution: where should the matching requests be sent;
  • [flags]: options affecting the rewritten request.

Let's start with the following snippet:

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

First line of code, describes RewriteEngine is turned on. Here I use RewriteCond directive which defines a rewrite rule condition. Using RewriteCond directive we defined two conditions here. This directive took a server variable called REQUEST_FILENAME. The two conditions above tell if the request is not a file or a directory, then meet the rule set by RewriteRule. See more details on this issue.

Now it's time to write some rewrite rules. Let's convert

www.example.com/?pr=project-abc123

// to 

www.example.com/project-abc123

and rewrite rule will be:

RewriteRule ^(.*)$ index.php/?pr=$1 [L]

And to get the www.example.com/pr/project-abc123 we need the rule as the below:

RewriteRule ^/?([a-z]+)/(.*)$ index.php/?$1=$2 [L]

// or

RewriteRule ^/?pr/(.*)$ index.php/?pr=$1 [L]

The [L] flag tells mod_rewrite to stop processing the rule set. This means that if the rule matches, no further rules will be processed.

unclexo
  • 3,691
  • 2
  • 18
  • 26
  • Well, It works fine in my end. Did you check your mod_rewrite is enabled? Which version of apache are you using? Is your `MultiViews` enabled? If so, disable it like `Options -MultiViews`. – unclexo Feb 26 '20 at 05:00
  • I did it. thank you. The problem was htaccess instructions that conflict with these. 'MultiViews' is important? – spanboy Feb 26 '20 at 14:04
  • Yes it is when content negotiation is important. Check out this answer https://stackoverflow.com/a/25423722/7935051 – unclexo Feb 26 '20 at 16:43