.htaccess url rewrite for a specific url - regex

I have a pattern like,
www.example.com/api.php/1,
how do I remove .php from this url,
currently I have this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But it only works if I put www.example.com/api in browser,
not for www.example.com/api/1
not even for www.example.com/api/

You may use a rule like this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([\w-]+)(/.*)?$ $1.php$2 [L]

Related

htaccess mod rewrite - rewrite rule

I would like to ask for help with the following: I have for example this rule mod_rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
I want to rewrite the URL www.x.cz/word to www.x.cz/word.php
This works, but problem arise in that case, if the URL is for example www.x.cz/word/xxx (shortly after another slash is some other text string). Then there is error 500. How could i solve this problem? (I would like the server to return 404 or do the same as referring to the URL www.x.cz/word.php - as if there was not the another string).
Thank you all for your willingness.
You can tweak your regex like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^./]+) $1.php [L]
This will rewrite both /word/xxx and /word/ to /word.php

how to hide file extension from the middle of the url using htaccess

I have a problem with hide URL in .htaccess. I want to remove .php from the URL.
For Example: convert www.example.com/destination_info.php/Laos-destination-trip to www.example.com/destination_info/Laos-destination-trip
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/(.+)$ $1.php/$2 [L]
To hide .php use following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To hide html
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Try this, To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A reference to the full article can be found here
If you want to use the rule in the middle of the URL for an API like in my case, then use the first answer from #anubhava. That worked for me.
Also, I'm using IIS so I had to install the Rewrite Module and then Import the rule from the first answer. Second and Third answers will work only for the last part of the URL.

Remove parameter from URL

I have URL on my localhost like this:
http://localhost/mysite/index.php?id=8
I want to remove index.php?id= from the URL and show it like this:
http://localhost/mysite/8
Or:
http://localhost/mysite/about
With some character in place of integer 8.
You can use this rule in /mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{THE_REQUEST} /index\.php\?id=([0-9]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L,QSA]
You're misunderstanding mod-rewrite.
You must use short URLs everywhere in your code (mysite/8), and let your .htaccess redirect these fake URLs to the real one (mysite/index.php?id=8).
You can not expect a .htaccess to beautify your URLs magically.
That being said, the .htaccess you're looking for is :
RewriteEngine On
RewriteBase /mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [L]

Rewrite URL if page exists in a sub folder

I was wondering if there is a way to achieve this sort of URL rewriting;
RewriteRule ^example-link-1?$ /blog/example-link-1 [NC,L]
RewriteRule ^example-link-2?$ /blog/example-link-2 [NC,L]
RewriteRule ^example-link-3?$ /blog/example-link-3 [NC,L]
RewriteRule ^example-link-4?$ /blog/example-link-4 [NC,L]
where you go directly to a link such as example.com/example-link-1 and you are actually shown content from example.com/blog/example-link-1, but without the need for a new rule per page i add. Is there a dynamic way to do this in the htaccess? ie;
IF exists('blog/'+pageURI)
THEN rewrite to 'blog/'+pageURI but keep the uri without 'blog/' in it.
Yes sure you can do:
RewriteRule ^(example-link-[1234])/?$ /blog/$1 [NC,L]
for above 4 rules. But in general you can do this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/blog/$1 -f
RewriteRule ^(.+?)/?$ /blog/$1 [NC,L]
This will rewrite to /blog/something if /blog/something exists while keeping the same URI without /blog/.

mod_rewrite - Only use whatever is after the last forward slash

So I already have my .htaccess file configured so that when I load the webpage (http://example.com/about), it transparently redirects it to http://example.com/about.html, however, I'd like it now to be able to do (http://example.com/about/contact), then have it transparently redirect to http://example.com/about/contact.html. I guess ideally it'd completely ignore the "/about/" part of the URL and only use the contact part.
This is what I'm using right now:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Use this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
# folder/file => folder/file.html
RewriteRule ^([a-z0-9_\-]+)/([a-z0-9_\-]+)/?$ $2.html [NC,L]
# file => file.html
RewriteRule ^([a-z0-9_\-]+)/?$ $1.html [NC,L]