.htaccess url rewriting is working but not the redirection - apache

I'm working on a local server on an html/php application and I'm trying to use the Apache url rewrite module without success
The application was stored in ./Compta/index.php. I have to an .htaccess file in ./Compta/.htaccess
I would like to only use a rewritten url like :http://localhost/Compta/Test/
instead of : http://localhost/Compta/index.php?page=test
and redirect users if they try to go to the old url
The .htaccess file contains :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L]
RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301]
When I go to http://localhost/Compta/Test/ the following line is working and my code includes in a div the content of test.php :
RewriteRule ^([[:alnum:]]*)/$ /Compta/index.php?page=$1 [L]
When I go to http://localhost/Compta/Test the following line is working but in firefox the url is rewritten to http://localhost/Compta/index.php?page=Test and this is not happening with http://localhost/Compta/Test2; the url isn't rewritten.
RewriteRule ^([[:alnum:]]*)$ /Compta/index.php?page=$1 [L]
To fix this and to redirect the old url I added these lines :
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=([[:alnum:]]*)$
RewriteRule ^(.*)$ http://localhost/Compta/%1/ [L,R=301]
But this is not working and when I go to http://localhost/Compta/index.php?page=Test the url isn't rewritten to http://localhost/Compta/Test/
Thank you in advance

I didn't find a solution with .htaccess but i found one with php so i added the following lines in top of my php files :
if(preg_match("/\/Compta\/index\.php\?page=([[:alnum:]]*)/",#$_SERVER['REQUEST_URI'],$page)&&!empty($page[1]))
{
$new_url = "/Compta/";
switch (strtolower($page[1])) {
case "test":
$new_url = $new_url."Test/";
break;
case "test2":
$new_url = $new_url."Test2/";
break;
default:break;
}
header("Status: 301 Moved Permanently", false, 301);
header("Location: ".$new_url);
exit;
}
I test if the url begin with "/Compta/index.php?page=" AND if there is a parameter for "page"
Then I initialise the variable which contain the new url
Switch the content of my parameter I modify the new url
and then I make the redirection to my new url :)

Related

Rewrite parameter url to normal url but it should get original url parameters after redirect

Original URL:
https://www.example.com/all_users/user.php?id=1&subtype=p
Expected URL:
https://www.example.com/user/john-doe.html
I tried using following rule, it redirects to expected URL but I can't use parameters (id and subtype) with new URL.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id\=1&subtype\=p$
RewriteRule ^all_users/user\.php$ https://www.example.com/user/john-doe.html? [R=301,L]
How can I use parameter values with new URL.
Example:
After redirect, new URL will be https://www.example.com/user/john-doe.html
Then I should able to get original parameters value with new URL.
$id = $_GET['id'];
$type = $_GET['subtype'];
With your shown samples and attempts, please try following htaccess rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect here.
RewriteCond %{THE_REQUEST} \s/all_users/user\.php\?id=\d+&subtype=p\s [NC]
RewriteRule ^ user/john-doe.html? [R=301,L]
##Internal rewrite from here..
RewriteRule ^user/john-doe\.html/?$ all_users/user.php?id=1&subtype=p [QSA,NC,L]

how to redirect "www.somedomain.com/folder1/demo.jpg" request to some other page using mod_rewrite?

code block:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} mysite.000webhostapp.com$ [NC]
RewriteCond %{HTTP_HOST} !folder1
RewriteRule ^(.jpg)$ http://mysite.00xxxtapp.com/folder1/$1 [R=301,L]
I think you want your filename in the new url as well? (not just ".jpg")
RewriteRule ([^/]+\.jpg)$ http://mysite.00xxxtapp.com/folder1/$1 [R=301,L]
or
RewriteRule ([^/]+\.jpg)$ /folder1/$1 [L]
if you want to keep seeing the old url
EDIT
So you need more than 1 rewriterule.
I suggest the steps
all "fake" urls redirect to some php script
all images in /folder1/ must be redirected or be made inaccessable (suggestion: put them outside the document root|)
RewriteEngine On
# all images in somefolder will be parsed through a php script
RewriteRule somefolder/([^/]+\.jpg)$ /imageshower.php [L]
# redirect to a no access image
RewriteRule folder/*\.jpg /no-access.jpg [R]
The php script will be able to do some checking like 'is the user logged in?' or does he have the rights
<?php
$filename = basename($_SERVER['REQUEST_URI']); // this still points to the jpg
if(is_file('folder/'. $filename) && is_allowed($user, $filename)) {
header('Content-Type: images/jpg'); // or other like .gif or .pdf etc
readfile('folder/'. $filename);
}
?>

Redirection by querying database value like stackoverflow.com URLs

So I use the following htaccess code to redirect the URLs to clean looking URLs but, now what I need to happen is to have the original URLs to redirect to the new clean URLS.
Example
Original URL:
example.com/search/store_info.php?store=113&dentist=Dr.%20John%20Doe
Clean URL:
example.com/search/113/dr-john-doe
What I need is the "ORIGINAL URL" to redirect to the "CLEAN URL". Reason why I need this to happen is that both URLs are showing up in Google searches.
I only want the clean URL to show up and anytime the original URL is used it should automatically redirect to the clean URL. It currently doesn't do that.
Here is my htaccess file.
ErrorDocument 404 default
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /search/
RewriteCond %{QUERY_STRING} .
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ [NC]
RewriteRule ^ - [L]
RewriteRule ^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$ $1/$2-$3 [NC,DPI,E=DONE:1]
RewriteCond %{ENV:DONE} =1
RewriteRule ^([0-9a-z]+)/([^\s.]+)$ $1/$2 [R=301,NE,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9-]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]
</IfModule>
I've read about RedirectMatch however I don't know where to implement it into my htaccess file.
Inside your store_info.php have code like this:
<?php
$store = $_GET['store'];
if (empty($store)) {
header("HTTP/1.1 404 Not Found");
exit;
}
$dentist = $_GET['dentist']);
// pull dentist from DB by store id. Create this function in PHP and
// query your database
$dentistFromDB = preg_replace('/[\h.]+/', '-', $row['businessName']);
// DB returns an empty or no value
if (empty($dentistFromDB)) {
header("HTTP/1.1 404 Not Found");
exit;
}
// DB returned value doesn't match the value in URL
if ($dentistFromDB != $dentist) {
// redirect with 301 to correct /<store>/<dentist> page
header ('HTTP/1.1 301 Moved Permanently');
header('Location: /' . $store . '/' . $dentistFromDB);
exit;
}
// rest of the PHP code should come now
?>
You do not need to clutter your htaccess file anymore with arbitrary rules/directives. The current set of rules, with the %{THE_REQUEST} matching, and later with the %{ENV=DONE} conditionals are more than enough.
Just wait a few days for google to crawl your pages again, and the older unclean urls will disappear. You could also search around in Google Webmaster Tools to see if this can be triggered manually.
PS: You can remove the RewriteCond %{QUERY_STRING} . line from your first rule.

Apache htaccess sending URLs in a URL

I'm trying to send a URL in a URL, and there's ModRewrite in the middle.
Here's the Rewrite Rule. It's used to search for a user's profile.
RewriteEngine On
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
And this is the URL
http://www.example.com/query/profile/http://www.facebook.com/example
No matter how I do the rewrite rule, I keep getting NetworkError: 500 Internal Server Error or NetworkError: 404 Not Found if it's a URL I put there.
How can I do this right? I'm on Windows 7 Home Premium. Am I missing something?
The URL above is passed using ajax.
Edit:
#htaccess in site1 folder
RewriteRule ^resources/?(.*)/?$ /control/$1 [NC,L]
#htaccess in control folder
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
next folder is `search` which has `index.php`
URL used
http://www.example.com/resources/query/profile/http://www.facebook.com/example
EDIT:
I redid the RewriteRule like below, and don't get any error messages now, but Apache seems to be taking off one / from the url after the http://. So I get only http:/www.facebook.com/example. Any help with that will be appreciated.
RewriteRule ^(query)/(profile)/([a-z0-9\/:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]`
You need to capture URL from RewriteCond otherwise Apache will trim multiple // into a single one.
Change this rule in root .htaccess:
RewriteCond %{REQUEST_URI} /resources/(.+)$ [NC]
RewriteRule ^ /control/%1 [L]
Have this in /control/.htaccess file:
RewriteEngine On
RewriteBase /control/
RewriteCond %{REQUEST_URI} /(query)/(profile)/(.+)$ [NC]
RewriteRule ^ /resources/search/index.php?scope=%2&query=%3 [QSA,L]

mod_rewrite condition for existing folder

I'm trying to set-up an .htaccess file that will pass every request URL as GET into a file called index.php. The only exception is, when the request URL points to a directory res.
Examples:
/wiki/cool-stuff => index.php?uri=/wiki/cool-stuff
/wiki/cool-stuff?news=on => index.php?uri=/wiki/cool-stuff&news=on
/res/cool-photo.jpg => res/cool-photo.jpg
Two problems:
The GET variable passed to /wiki/cool-stuff in the second example is not passed to index.php
Accessing /res (not /res/!!) suddenly shows me /res/?uri=res in my browser and index.php with uri=res/. Accessing /res/ instead, shows me index.php with uri=res/ and the URL in the browser stays (which is okay).
The .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res/(.+)$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1
How can I achieve the desired behaviour?
Try using the Query-String-Append flag, QSA
Make the trailing slash optional - in Regex, this is achieved by adding ?.
New .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res(/.*)?$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1 [QSA]
Note that I have tweaked the Regex on the /res folder to cause /resabc to be redirected (if the slash was the only optional piece, anything beginning with res would match.
Apache Mod_Rewrite Documentation