GenesisBits
GenesisBits

Reputation: 346

URL rewrite when .php extension is already hidden

I have a URL that has already been rewritten to hide my .php extensions. I am now having issues when trying to further rewrite the URL.

My current URLs (note the forward slash before the query string):

https://example.com/card2/?search=Jimmmy 

Here is what I have currently tried:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2.php?search=$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2.php\?search=([^\&\ ]+)
RewriteRule ^/?card2\.php$ /card2/%1? [L,R=301,NE]

This works if I try this URL (I have to manually remove the forward slash before the query string and type in .php):

https://example.com/card2.php?search=Jimmy

So the only way I can get it to work at the moment is by typing in .php in the URL and removing the forward slash after that.

I have tried the following, my logic was to just remove the .php extensions that were in the rewrite and add the forward slash too but it doesn't work:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /card2\/\?search=([^\&\ ]+)
RewriteRule ^/?card2$ /card2/%1? [L,R=301,NE]

EDIT, thanks to anubhava for the answer, here is my htaccess file now:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
ErrorDocument 404 /not-found.php

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

<filesMatch "\.( jpg|jpeg|gif|png|ico|js)$">
Header set Cache-Control "max-age=2419200, public, must-revalidate"
</filesMatch>

<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 70.24.57.210

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?card2/(.*?)/?$ /card2/?search=$1 [L]

RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]

Upvotes: 1

Views: 65

Answers (1)

anubhava
anubhava

Reputation: 786339

Have your redirect rule like this with optional matches:

RewriteCond %{THE_REQUEST} \s/card2(?:\.php)?/?\?search=([^\&\s]+) [NC]
RewriteRule ^ /card2/%1? [L,R=301,NE]

Also keep this this rule at top of your .htaccess and test from a new browser.

Upvotes: 1

Related Questions