Rewrite URL APACHE 2.4.9 htaccess - php

i am using apache 2.4.9
i want to rewrite a url in htaccess
https://localhost/balance.php/editbalance
when i enter above url it should be displayed like below
https://localhost/editerapi/editbalance
i have tried this but its not working
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^editerapi/?$ balance.php [NC,L]
RewriteCond %{REQUEST_URI} ^/editerapi [NC]
RewriteRule ^.*/([^/]+)/? balance.php/$1 [L] #POST
MY REWRITE ENGINE IS ON
the below code of hiding php extension is working
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [QSA,L]

In order to show the new URL in the browser's bar, you must redirect instead of rewrite, e.g. use the R|redirect flag
RewriteRule ^balance\.php/(.*)$ /editerapi/$1 [R,L]
Now the client requests the new URL /editerapi/..., and you must rewrite to
RewriteRule ^editerapi/(.*)$ /balance.php/$1 [L]
To prevent a rewrite loop, a RewriteCond is added. Now the redirect only happens, when balance.php is requested
RewriteCond %{THE_REQUEST} " /balance\.php/"
Everything put together gives
RewriteRule ^editerapi/(.*)$ /balance.php/$1 [L]
RewriteCond %{THE_REQUEST} " /balance\.php/"
RewriteRule ^balance\.php/(.*)$ /editerapi/$1 [R,L]

Related

Rewriting "pretty links" causng 500 Internal Server Error

After much research, I've finally learned that mod_rewrite does not create "pretty links", rather it rewrites them to "normal" links that get processed by the server.
With some generous help here on SO, I have been able to do some basic URL rewriting, specifically removing the www. and also the .php file extension even when a user types in page.php. The .htaccess code for reference:
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
This website I'm working on has a news section, the links use a query string to pass the article id from links in the sidebar like so:
<a href="news.php?id=' . $phpVariable . '...etc.
After finding this amazing post HERE I began experimenting with this line of code from the post:
RewriteRule ^blog/([0-9]+)/([A-Za-z0-9-\+]+)/?$ /blog/index.php?id=$1&title=$2 [NC,L,QSA]
and modified it to match my website like so:
RewriteRule ^news/([0-9]+)$ news?id=$1 [NC,L,QSA]
Then I modified the link code in my news section to this:
<a href="news/' . $phpVariable . '/...etc.
so that the link would be "pretty" and be rewritten with the modified RewriteRule shown above.
The new RewriteRule was placed at the bottom of the .htaccess code (which may be part of the problem?) shown here:
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite pretty link to fetch news article
RewriteRule ^news/([0-9]+)$ news?id=$1 [NC,L,QSA]
Now when the news section is tested, any item selected from the sidebar causes a 500 Internal Server Error.
I'm just a bit in over my head, though thanks to the information here on SO, the regex is beginning to make more and more sense to me.
Based on question and comments I will suggest you to have it this way:
ErrorDocument 404 default
ErrorDocument 500 default
Options +FollowSymLinks -MultiViews
RewriteEngine On
# omit www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# rewrite pretty link to fetch news article
RewriteRule ^news/(\d+)/?$ news.php?id=$1 [NC,L,QSA]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

.htaccess not converting the dirty url into clean url

I am having an URL named www.example.com/print?id=adder
I want it to look like www.example.com/print/adder
I am using the following .htacces code
RewriteEngine on
# redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^print/([a-z]+) print.php?id=$1 [NC,L]
But still, i am having the URL like www.example.com/print?id=adder.
I am using wamp 3.0.6(Latest Version), PHP 7.0,10 & Apache 2.4.23. Why am i facing this problem. Why isn't the code working?
To redirect your url to cleaner version, put this above your last RewriteRule
RewriteCond %{THE_REQUEST} /print/?(?:\.php)?\?id=([^\s&]+)
RewriteRule ^ /print/%1? [L,R]

htaccess- unable to remove file extension from url php

I am trying the following rules to remove file extension from file name but the problem is it is showing that it is removed but when I login again it is not redirecting to my home page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase \
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
Thank You
In your redirection just type in page name with no extension, eg. header('Location: index'); or redirect_user('index'); and use the code below in your .htaccess file. now the link on the browser will be say "localhost/index"
RewriteEngine on
RewriteRule ^([^./]{3}[^.]*)$ /index.php?page=$1 [QSA,L]
I think your rules are just a little bit off. You are using THE_REQUEST to redirect php extension to non php extension but you are not using a / after %1 in the rewriterule so when the next rule is read it is not matching because it will only internally redirect if there is a / in that rule. So either add a forward slash after %1 in the second rule or make the backslash optional in the last rule. I would just make it optional in last rule so it will match either way.
Try this update and see how it works.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /([^&\ ]+).php [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [NC,L]

htaccess for changing index.php?ms=user to /user

I want to redirect my site url's with 301 code
http://www.domain.com/index.php?ms=user&ms_1=name --> http://www.domain.com/user/name
http://www.domain.com/index.php?ms=2652&ms_1=title --> http://www.domain.com/2652/title
http://www.domain.com/index.php?ms=questions --> http://www.domain.com/questions
http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc --> http://www.domain.com/aaa/bbb/ccc
the stuffs after ms can have space and utf8 characters too
How can I do it ?
This should work
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)&ms_1=(.*)&ms_2=(.*)\ HTTP
RewriteRule ^ /%2/%3/%4\? [R=301,L]
RewriteRule ^(.*)/(.*)/(.*)$ /index.php?ms=$1&ms_1=$2&ms_2=$3 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)&ms_1=(.*)\ HTTP
RewriteRule ^ /%2/%3\? [R=301,L]
RewriteRule ^(.*)/(.*)$ /index.php?ms=$1&ms_1=$2 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)\ HTTP
RewriteRule ^ /%2\? [R=301,L]
RewriteRule ^(.*)/$ /index.php?ms=$1 [L]
It will change http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc into http://www.domain.com/aaa/bbb/ccc, but all you to see the content of http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc etc for the other two links you have.
I believe you can enable Apache rewrite module and give rewrite rules to do it. It has regex with back-reference ability.
A possible (cause I'm not that familiar with regex in rewrite module) ruleset could be :
ReWriteRule ^/index.php?ms=(.+) /$1
ReWriteRule ^/index.php?ms=(.+)&ms_1=(.+) /$1/$2
ReWriteRule ^/index.php?ms=(.+)&ms_1=(.+)&ms_2=(.+) /$1/$2/$3
please follow the code i hope it will be help to you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?ms=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?ms=$1&ms_1=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?ms=$1&ms_1=$2&ms_2=$3 [L]
befor this code apache rewrite module is enable

htaccess url rewrite wont allow other pages

I have a script thanks to Howlin that will rewrite my url, however, I cannot access any other page then, it just gives a 404 error?
The rewrite rewrites localhost/docci.me/profile.php?user=person to localhost/docci.me/person and it works, but as soon as I try to load logout.php, index.php, or any other page, it gives a 404. If I take the .htaccess code out, it works fine but obviously does not rewrite the URL like I need it to.
here is the code:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /docci.me/login/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /docci.me/login/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^(.*)$ /docci.me/login/profile.php?user=$1 [L]
Place this in /docci.me/.htaccess:
RewriteEngine On
RewriteBase /docci.me/
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /login/profile\.php\?user=([^&\s]+) [NC]
RewriteRule ^ login/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^./]+)/?$ login/profile.php?user=$1 [L,QSA]

Categories