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]
Related
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...
How do I make it so that my page redirects a url like: https://mysite/12345 to https://mysite/image.php?id=12345
I want to be able to shorten urls to only include the get request. Is this achievable through php or a .htaccess file?
Typically you would do this using the .htaccess file, with entries like this:
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)(/?)$ image.php?id=$1 [NC,L]
So you can write your urls in the format:
https://mysite/12345
To go the other way and have a url such as https://mysite/image.php?id=12345 redirect to https://mysite/12345
RewriteCond %{THE_REQUEST} /(?:image\.php)?\?id=([^&\s]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
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.
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
Well I'm making profile pages so right now it looks like this
http://example.com/random/?user=Robert
What I want to do is remove ?user= from the URL so the page appears as
http://example.com/random/Robert
Iv'e searched and I can't find anything working for me.
Thanks!
Based on http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html this should do the trick:
RewriteCond %{QUERY_STRING} ^user=(.*)$ [NC]
RewriteRule ^random$ random/$1 [NC,L,R=301]
The first step is to make all of your links in this form http://example.com/random/Robert, then in the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^random/(.+)$ /random/?user=$1 [L]
But to handle 301 redirects to your old URLs, you can include this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /random/\?user=([^\ ]+)
RewriteRule ^random/$ /random/%1 [R=301,L]