Htaccess how to invert extension and name - .htaccess

I tried to invert the name and the extension of a requested file, but unsuccesfully.
RewriteRule ^(.*)\.(.*)$ $2.$1
I tried that, but is not working.

Try:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*)([^/]+)\.([^/]+)$ /$1$3.$2 [L]

Related

how to hide id = from url using htaccess file

I need the url
from
localhost/project/category?c=electronics
to
localhost/project/category/electronics
I have tried
RewriteRule ^category/([^/\.]+)?$ /category.php?c=$1 [L]
RewriteRule ^category/+?$ /category.php?c=$1 [NC,L]
With your shown samples and attempts please try following htaccess rules. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##External redirect to url change in browser.
RewriteCond %{THE_REQUEST} \s/(project/category)\.php\?c=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to category.php in backend.
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ %{DOCUMENT_ROOT}/$1/$2.php?c=$3 [QSA,L]
RewriteEngine on
RewriteBase /
RewriteRule ^project/category/([0-9a-z]+)$ /project/category?c=$1 [L]
Why is "project/" missing in your original try ?
You have to specify the full path.
You can try this simple rewriteRule wich should works.

Rewriterule for PHP files fails

I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
For example:
RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L]
RewriteRule ^admin/([a-z-])/([0-9a-z-#._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L]
I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error
The requested URL was not found on this server.
The full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
Files locations are:
1. C:\wamp64\www\test.com
2. C:\wamp64\www\test.com\admin\add_news.php
C:\wamp64\www\test.com\admin\edit_news.php
3. I want redirect from URLs with underscores to hyphens, for example:
from admin/add_news.php to admin/add-news
Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id
Example: admin/edit-news/my-news-title/2
Any ideas how to fix it? Thank you.
With your shown attempts, please try following htaccess rules file.
Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.
Samples specific rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L]
Generic rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L]

REQUEST_FILENAME without extension in RewriteCond

Is there any mode_rewrite variable like {REQUEST_FILENAME} but without the file extension?
The following works but I need to change my image naming:
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule \.(jpe?g|png)$ %{REQUEST_FILENAME}.webp [NC,L]
But now i need to change my image naming from: "image.jpg.webp" to "image_jpg.webp".
I need help with the new RewriteCond and RewriteRule in order to rewrite "image.jpg" to "image_jpg.webp" if the webp image exists.
Just capture value in RewriteRule and use it in RewriteCond:
RewriteCond %{REQUEST_URI} ^(.*/)([^/]+)\.(jpe?g|png)$ [NC]
RewriteCond %{DOCUMENT_ROOT}%1%2_%3.webp -f
RewriteRule ^ %1%2_%3.webp [L]

change site path by RewriteRule3

i need change the site url path
from
site.net/open.php?cat=22&book=2285
to
site.net/book/2285
i think this code need edit and complite
RewriteEngine On
RewriteRule ^book/([^/]*)$ /books/open.php?cat=22&book=$1 [L]
how to do it by .htaccess and RewriteRule?
i need code work in all categorys also not 22 only
This code should work for you -
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)cat\=22($|&)
RewriteCond %{QUERY_STRING} (^|&)book\=2285($|&)
RewriteRule ^open\.php$ /book/2285? [L,R=301]

Removing index.php from URL but keep old links working

I'm trying to remove the index.php from the URL, which is working with the .htaccess examples found on the EZPublish site and ForceVirtualHost=true. The problem is that the old links that point to index.php are no longer working (which is problematic when linking from search engines).
I've tried to find a fix for this in using rewrite rules in .htaccess, but I can't get this to work. Some of my attempts are:
Attempt 1
RewriteCond %{REQUEST_URI} ^/index.php
RewriteRule ^index\.php(.*) http://www.mysite.com$1
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This attempt causes an infinite loop.
Attempt 2
RewriteCond %{REQUEST_URI} ^/(index\.php)?(.*)$
RewriteRule %2 index.php [L]
RewriteRule .* index.php [L]
Also doesn't work :-(.
NB. 'RewriteRule .* index.php [L]' is necessary to make the virtual host setup in EZPublish work.
Any help would be greatly appreciated.
Vivienne
in your settings/override/site.ini.append.php:
[SiteAccessSettings]
ForceVirtualHost=true
Try to check the request line instead:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php/?([^/].*)?$ /$1 [L,R=301]
RewriteRule !^index\.php$ index.php [L]

Resources