Shorten URL to only include get request? - php

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]

Related

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]

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

url rewrite and redirect with folder using htaccess

OK I'm new with the issue of URL rewriting and redirecting. I've search up and down on Google & Stackoverflow for an answer that would work and nothing seems to work!!
What i'm trying to accomplish is...
Take this link: mysite.com/research/index.php?quote=goog
Then convert it and redirect it to this: mysite.com/research/goog
Does it matter that the "goog" at the end of the URL string is grabbed from a form placed in the url? here is the code used to grab the "goog" <?php echo $_POST['quote'];?>
Below is the only snippet code that I have on my htaccess file and it won't work! Am I doing it wrong? Is there something missing from my code? I have my code place on the root directory (mysite.com) should i have it in the "research" folder? (mysite.com/research/)
RewriteEngine On
RewriteRule ^quote/([^/]*)$ /research/index.php?quote=$1 [L]
Is it possible my host / server doesn't accept .htaccess files? should I do it in a web.config file? If so how would I convert the above code to a working web.config file?
These are the rules you will need in your /research/.htaccess file:
RewriteEngine On
RewriteBase /research/
RewriteCond %{THE_REQUEST} /index\.php\?quote=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?quote=$1 [L,QSA,NC]
Access it in your index.php using:
$quote = $_GET['quote'];`
This should work for you:
RewriteEngine On
RewriteCond %{QUERY_STRING} "quote=([^&]+)"
RewriteRule ^/research/index.php /research/%1? [R,L]
The query string is a separate part of the request to the URL so you need to check that explicitly. By using the RewriteCond, you can say if the 'quote' parameter exists, you'll capture it's value (by getting all characters that follow it that are not '&'). This populates the %1 variable which you can use in the rewrite itself.

.htaccess Remove PHP get data from 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]

passing a query string to URL using .htaccess

I am trying to add the page title into the URL. Just like this site does! Basically I want to rewrite the url from
www.siteurl.co.uk/project-82
to
www.siteurl.co.uk/project/the-title-of-the-project
The string that is passed from the php to html file is {PROJECT_TITLE}. How would I add this in the .htaccess file? Thanks guys!
EDIT:
This is my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project\.php [NC]
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule . project-%1? [R=301]
RewriteEngine On
RewriteRule ^project/([a-z-]+)/?$ index.php?project_title=$1
With the above .htaccess, when accessing
www.siteurl.co.uk/project/the-title-of-the-project
You will in your PHP script have a variable $_GET['project_title'] containing "the-title-of-the-project".

Categories