clean URL's with htaccess from get request - php

I'm trying to rewrite some url's from a $_GET request with .htaccess with no luck.
How's the syntax for (left side = original url, right side = new url)
/?lang=en ---> /en/
/index?lang=en ----> /en/index
/pageA?lang=de ----> /de/pageA
/pageB/lang=es ----> /es/pageB
Can this be done without editing manually all the pages and get requests in the .htaccess file?
Thanks in advance for helping.
EDIT
This is my code so far to eliminate the .php at the end in the URL
Options -Indexes +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
It's only the code which is working.

Something like this could work:
RewriteRule ^([a-z]+)/([a-zA-Z]+)/$ /$2?lang=$1 [QSA]

This is what I came up with:
RewriteRule ^(([-A-Za-z0-9_]+)?(\?|\/)lang=([A-Za-z]{2}))$ redirect.php?page=$1 [L]
EDIT:
If you want to redirect to the clean URL, then what you probably want to do is make one central php page to go to. From that page, determine where you want to go and then do a header("Location: ".$real_page); exit;
Here's an example:
<?php
// REDIRECT.PHP
$page = $_GET['page'];
$real_page = preg_replace('/([-A-Z0-9_]+)?(\?|\/)lang=([A-Z]{2})/i', '/$3/$1', $page);
header("Location: ".$real_page);
exit;
EDIT 2:
If you want to keep it all withing .htaccess and not mess with the PHP redirect, you can use the [R=302] flag.
RewriteRule ^([-A-Za-z0-9_]+)?(\?|\/)lang=([A-Za-z]{2})$ /$3/$1 [R=302]
That should actually change the URL for you.

Related

How to remove extension .php in the middle of URL

Here is my URL:
http://localhost/school-project/project1/mypage.php/home
I wanna get rid of .php in mypage.php. So the new URL should look like this:
http://localhost/school-project/project1/mypage/home
I have tried to use RewriteRule in .htaccess, but none of them worked!
Here is the code in my .htaccess:
(this one actually gets rid of the .php, but it turned the page to Object not found, error 404)
RewriteRule ^mypage.php/(.*)$ http://localhost/school-project/project1/mypage/$1[NC,L,R]
or
RewriteRule (.*)mypage/(.*)$ /mypage.php?/$1 [L]
I don't really know where the problem is. Any ideas?
Thanks!
Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*)\.php(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_URI} !\.php
RewriteRule ^([^\/]*)/([^\/]*)$ $1.php/$2 [L]
Second & third lines are removing php extension externally.
Forth & fifth to redirect request to original path internally .
Clear browser cache then test , if it is Ok change R=302 to R=301 in order to be permanent redirection .
I found another solution for my problem. Thank you so much for all the help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^mypage(/.+)$ mypage.php$1 [NC,L]

htaccess avoid GET vars(? and & chars)

I want to avoid ? and & chars from the URL.
For example my php file is like this:
<?php
$_GET["page"] = "page1";
?>
And, htaccess file:
RewriteRule ^page1$ index.php?page=page1
I want to give access to users only by typing www.example.com/page1 not by typing www.example.com/index.php?page=page1 .
Is there any way to make it?
Thanks
Have another 301 rule on top to redirect users from your old URL to new pretty URL:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?page=([^\s&]+)\s [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteRule ^page1/?$ index.php?page=page1 [L,QSA,NC]
example:
RewriteRule ^page/([0-9]+)? index.php?page=$1 [L]

mod rewrite url with two variables

I want to rewrite post.php?id=1&title=test to post/1/test
I have this code:
RewriteRule ^post/([^/]+)/([^/]+)/?$ post.php?id=$1&title=$2 [L,QSA]
This only works if I rename post.php links to /post in my php file. I want to rewrite the urls so that I don't need to edit any links.
Similar to below:
RewriteCond %{THE_REQUEST} \s/+(post)\.php[?/\s] [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^(post)/?$ $1.php [L]
You can use:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /post/%1/%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^post/([^/]+)/([^/]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
But it is wrong to say "I don't need to edit any links". Because it is a method to correct the old links already referenced, not to prevent you from changing your code in your pages. Because HTML code still contains bad links...

Hide .php extention from urls with query string

I am using .htaccess file for url rewriting . I've written all pages with .php extention and to hide them i am using this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]`
But this hides when i write "www.example.com/abc.php" to "www.example.com/abc" . I want them to change automatically like when i click on a link
example
then page link should open like "www.example.com/example" not "www.example.com/example.php"
Also how to hide "?var="hello" from all pages but variable should be sent to that page is that possible ?.
UPDATE
For example here i am using this
<?php
if(isset($_GET['hi'])){
echo $_GET['hi'];
}
?>
a<br>
b<br>
c<br>
d<br>
I want what ever the page is should be accessed without extension and query string should be hided but must be accessed.
Use this .htaccess:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
For hidding ?val you should use POST instead of GET. And take a look on this article for .htaccess he has defined good in here.
Regarding hiding the query string. When the php page runs it can first check for a query string. If one is found you can pull the data out of the query string, store it somewhere (session probably) and redirect to the page with no query string (and no file extension for that matter).
However if you do this you're obscuring most of the benefits of using a query string.
To remove the php extension completly, you can use :
RewriteEngine on
#1redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ %1 [NC,L,R]
#if the request is not for a dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing php file
RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite the request to orignal php file
RewriteRule ^([^/]+)/?$ $1.php [NC,L]

.htaccess rewrite to produce clean url

Currently I set a rewrite rule like so, to produce clean and simple url's.
.htaccess
RewriteRule ^/about$ about.php [L]
But what i need to do is something a little different, the other way around. For example if a user clicks the following link
about
They would go to the about page /about
Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.
EDITED
I am using a pre-made script from phpdolphin, which is working fine. But the url are all index.php based and i would like to clean them up
Currently the only code within the .htaccess file
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
Add this rule before your existing rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]
You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user
RewriteCond %{QUERY_STRING} ^(.*)a=about(.*)$ [NC]
RewriteRule ^index.php /about [L,NC,R=301]
or you can use php header() function in index.php to redirect the request:
if ($_REQUEST['a'] == about) {
header('Location: /about');
exit;
}
Try this i checked and it works ...
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$ index.php?a=$1&q=$2 [L]
Note: add additional signs between [] if necessary
OR This
RewriteRule ^([^~]+)/([^~]+)/?$ index.php?a=$1&q=$2
I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url

Categories