Reputation: 869
I'm trying to add some rewrite conditions to laravel's public/.htaccess
file.
Here's the contents of the file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page(.*)\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /pages%1/%2? [R=302,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Testing this online here (link), gives me the desired results which is https://domain.com/page.php?id=1234 -> https://domain.com/pages/1234
However, when I put it up on my server, I'm getting redirected to this url instead https://domain.com/pages12345
.
It seems to me that the (.*)
on this line https://domain.com/campaign.php?id=12345
isn't being passed to %1
. Instead only the query string value is being passed.
Is it possible it's because of a difference in apache versions? How might I amend this?
Also, can someone comment on the code itself? What I'm trying to do here is redirect any url that matches /page*.php?id=1234
to pages*/1234
.
ie.
/page.php?id=1234 -> /pages/1234
/page-home.php?id=1234 -> /pages-home/1234
Upvotes: 1
Views: 213
Reputation: 786081
Change your rule to this:
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$ [NC]
RewriteRule ^(page)([^.]*)\.php$ /$1s$2/%1? [R=302,NC,L]
Make sure to clear your browser cache before testing this.
About regex:
^ # input start
(page) # match and group page in group #`
([^.]*) # match 0 or more of any char that is not a DOT and group it as group #2
\.php # match literal .php
$ # end of input
And replacement part:
/ # literal /
$1 # backreference to group #1 i.e. (page)
s # character s
$2 # backreference to group #1 i.e. ([^.]*)
/ # literal /
%1 # backreference to group #1 from RewriteCoond i.e. ([0-9]*)
? # ? is used to strip off query string
Upvotes: 1