How to rewrite URL.php in htaccess problem

Viewed 52

I need to rewrite every file in my .htaccess from .php to a clean URL without it, but I can't make it work.

My current code is the following:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} !/(blog)\.php
RewriteCond %{THE_REQUEST} \.phP
RewriteRule ^(.*)\.php$ /$1 [R=301,L]

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

Redirect 301 /url1.php /url2.php

ErrorDocument 404 /404.php

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 6 months"
</IfModule>

# Deflate Compression by FileType
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-shockwave-flash
</IfModule>

# Deflate Compression by MimeType</span>
<IfModule mod_deflate.c>
<FilesMatch ".(js|jpg|jpeg|gif|png|css|txt|html)$">
ExpiresActive on
ExpiresDefault "access plus 1 month"
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

When I want to access URL1 it gives a 404. It works by itself and with everything else but not when I add the php part at the beginning. I hope you can help me, thank you .

1 Answers

The following should work:

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]

The first Rewrite condition and rule combo redirects everything everything that ends with ".php" to the same address without it (it's stripping the .php from the URL). The second condition and rule combo remaps the requested resource back to load the php file (but the address will remain stripped).

Related