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]
Related
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]
i need following url to be changed as
http://example.com/Automobile/automotive_listing_subcategory?id=1&title=car-suv also need to rewrite urls such as: http://example.com/Automobile/automotive_listing_category?aid=1&title=car-suv
etc. these all pages under same folder. i had the following code.first one works.but if try to rewrite according to second condition it doesnt work. Anybody please help me.Am not an expert in this area. please correct me if there is any errors.
Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase /Automobile/
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+)\&title= ([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?id=$2&title=$3[QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?aid=([^&]+)&title=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)(/[^/]+)?/?$ $1.php?aid=$2 [L,QSA]
you can read https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ and what you need are regular expressions (regexes)
regexes are are chapter for themselves and very tricky to understand sometimes
they can also be used with linux commands like grep, sed,...
https://en.wikipedia.org/wiki/Regular_expression
the Apache server has a (url) rewrite engine: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
i am trying to rewrite URL using htaccess.
i need
1> temp.example.com/save.php?item=xxxxx&id=xxx&type=1
2> temp.example.com/save.php?item=xxxxx&id=xxx&type=2
to be like
1> temp.example.com/save/xxxxx/xxx
2> temp.example.com/save/xxxxx/xxx
have to hide last parameter .
my htaccess has
RewriteCond %{THE_REQUEST} \s/save\.php\?item=([_0-9a-zA-Z-]+)\s&id=([_0-9a- zA-Z-]+)\s&type=([_0-9a-zA-Z-]+)\s [NC]
RewriteRule ^temp/save/%1/%2/%3? [R=301,L]
RewriteRule ^temp/save/([_0-9a-zA-Z-]+)$/([_0-9a-zA-Z-]+)$/([_0-9a-zA-Z-]+)$ /save.php?item=$1&id=$2&type=$3 [L]
Thanks
You can use these rules in root .htaccess:
RewriteCond %{THE_REQUEST} /save\.php\?item=([^\s&]+)&id=([^\s&]+)&type=([^\s&]+)\s [NC]
RewriteRule ^ save/%1/%2/%3? [R=302,L]
RewriteRule ^save/([\w-]+)/([\w-]+)/([\w-]+)/?$ save.php?item=$1&id=$2&type=$3 [L,QSA,NC]
RewriteEngine On
RewriteRule ^save.php/([a-zA-Z0-9_-]+)/(.)/(.)$ /save.php?id=$1&order_id=$2&page_no=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
now you can access three parameter by pass like
temp.example.com/save/par1/par2
I've three url types which are as follows:
http://www.mywebsite.com/myprofile.php?user_username=username
http://www.mywebsite.com/input.php?user_username=username
http://www.mywebsite.com/users.php?user_username=username
Currently, users have to type the whole address like (http://www.mywebsite.com/myprofile.php?user_username=username) to go to their profiles and the same with input and users.
What I want is if a user types http://www.mywebsite.com/profile/username, he would be automatically redirected to http://www.mywebsite.com/myprofile.php?user_username=username.
When a user types http://www.mywebsite.com/input/username, he would be redirected to
http://www.mywebsite.com/input.php?user_username=username.
When a user types http://www.mywebsite.com/username, he would be redirected to http://www.mywebsite.com/users.php?user_username=username.
I know that this can only be achieved through .htaccess. However, I've searched but with no fruitful result.
Any help please!
Update
I think that the following code is close to being correct. However, I'm not getting the correct result and the css is also getting messed a lot. Individually they are working but together they are messing up. Any help please.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /myprofile\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /myprofile/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myprofile.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /user\.php\?user_username=(.*)\ HTTP
RewriteRule ^ //%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?user_username=$1 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /input\.php\?user_username=(.*)\ HTTP
RewriteRule ^ /input/%2\? [R=301,L]
RewriteCond %{QUERY_STRING} !user_username=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ input.php?user_username=$1 [L]
Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+input\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /input/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+myprofile\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /profile/%1? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+user\.php\?user_username=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [NC,QSA,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [NC,QSA,L]
Add these to the .htaccess file in your DOCUMENT_ROOT
RewriteEngine On
RewriteRule ^([^/]+)/?$ users.php?user_username=$1 [DPI,L]
RewriteRule ^input/([^/]+)/?$ input.php?user_username=$1 [DPI,L]
RewriteRule ^profile/([^/]+)/?$ myprofile.php?user_username=$1 [DPI,L]
Tested in Apache 2.2 and 2.4 :)
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All
I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:
website.com/about.php -> website.com/about
I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:
RewriteRule about about.php [L]
I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.
Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2).php$ https://website.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L,QSA]
You can use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
The below answer can be found here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]