passing a query string to URL using .htaccess - php

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".

Related

Redirect .jpeg files to .php files [duplicate]

Hi im trying to do redirect from .jpg file to specific URL address but it don't work. Can sb help me?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^/up/([\w]*\.jpg)$ /file?name=$1[R=301,L]
for eg.: i have image www.mydomain.com/up/image.jpg
and i would like to redirect it under: www.mydomain.com/file?name=image.jpg
Try this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^up/(.*)\.jpg/?$ /file?name=$1.jpg [R=301,L]
RewriteCond is totally redundant here, you can match this pattern in RewriteRule itself. Try this rule:
RewriteEngine On
RewriteBase /
RewriteRule ^up/(.+?\.jpe?g)$ file?name=$1 [NC,QSA,L]
Also you may not need R=301 here since you don't want to expose your internal handling of image to clients.

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.

Why doesn't this .htaccess code for a clean URL work?

I want to make a a URL that currently reads:
www.site.com/events/event.php?event_id=_n_
(where n is a number, naturally)
Read simply:
www.site.com/events/
I have the following code in my .htaccess file
RewriteEngine On
RewriteRule ^events/([a-zA-Z0-9]+)$ event.php?event_id=$1
But it doesn't seem to make any difference, regardless of whether I put the file in the root or the events directory. Any ideas?
Place these 2 rules in your DOCUMENT_ROOT/events/.htaccess file:
RewriteEngine On
RewriteBase /events/
RewriteCond %{THE_REQUEST} /event\.php\?event_id=([0-9]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteRule ^([0-9]+)/?$ event.php?event_id=$1 [L,QSA,NC]

.htaccess if request url diffrent from some string than

i have a trivial question about htaccess rewrite condition.
The think i want to do in my htaccess is:
When someone accesses my project from a url different from "example.com" to be redirected to a specific controller, for example app_user.php
I need the syntacs for this condition redirect.
Thanks for your time
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/app_user.php
RewriteRule ^ /app_user.php [L]

How to rewrite subsites with parameter to html only

My old website has a url like "/index.php?s=subsite" and "/?s=subsite".
Now I added the following lines to ".htaccess" file, to make them shorter:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.(html)?$ index.php?s=$1&%{QUERY_STRING} [L]
Open a site with "/subsite.html" works fine, but also the old url "/index.php?s=subsite" and "/?s=subsite" works too. Is there a way, to allow "/subsite.html" only and redirect the old requests to it?
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?s=([^\s]+) [NC]
RewriteRule ^ /%1.html? [R=301,L]
RewriteRule ^([^.]+)\.html$ /index.php?s=$1 [L,NC,QSA]
Add this rule to your htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^& \]+)&?([^\ ]*)
RewriteRule ^ /%1.html?%2 [L,R=301]
This matches against the request, tries to capture the s query string parameter, then redirects the browser to that parameter followed by an ".html" and the rest of any query string that was there.

Categories