.htaccess Remove first php url parameter if second exist

Viewed 94

This is my .htaccess code:

RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))? pages.php?PAGE=$1&LINK=$3 [L]

*where $1 = profile and $3 = john-smith

This is works fine rewriting like https://example.com/profile/john-smith but I need a second rewrite rule like https://example.com/john-smith only if the second parameter that contains john-smith exist.

Thank you!

PS. (Extra rules I have above in my .htaccess file)

# protect files beginning with .
RewriteRule /\.(.*) - [NC,F]

# redirect HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# No root access without index.* and other security
RewriteEngine On
Options All -Indexes
RewriteBase /
DirectoryIndex index.php index.html index.htm
ErrorDocument 404 https://example.com/pages.php?PAGE=404

# Prevent upload malicious PHP files
<FilesMatch “\.(php|php\.)$”> 
Order Allow,Deny 
Deny from all 
</FilesMatch>

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
1 Answers

With your shown samples, please try following. Written with shown samples/attempts. Please make sure your htaccess Rules file is present along with pages.php file.

Also make sure to clear your browser cache before testing your URLs.

RewriteEngine On
Options All -Indexes -Multiviews
RewriteBase /

# protect files beginning with .
RewriteRule ^\. - [NC,F]

# redirect HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)/?$ https://www.example.com/$1 [R,L]

##Rules for uri https://example.com/john-smith goes from here.
RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ pages.php?PAGE=$1 [QSA,L]

##Rules for uri https://example.com/profile/john-smith goes from here.
RewriteCond %{REQUEST_URI} !^/pages/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ pages.php?PAGE=$1&LINK=$2 [QSA,L]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

# No root access without index.* and other security
# Prevent upload malicious PHP files
<FilesMatch "\.(php|php\.)$"> 
Order Allow,Deny 
Deny from all 
</FilesMatch>

DirectoryIndex index.php index.html index.htm
ErrorDocument 404 https://example.com/pages.php?PAGE=404
Related