url extension remove with php - php

I have a dilemma here...
At the moment I'm launing a website and there are a lot of old url that needs to be redirected to it's new url without .stm extension. And the new site is running Durpal.
Ex.
http://www.foo.com/foo/bar.stm --> http://www.foo.com/foo/bar
So I went looking more into nginx.conf setting to rewrite the url before it even hit setting.php that is being used by drupal. Turns out that my host doesn't let me touch the conf file on the root folder. but my only solution is add this to the conf file in root
server { location / { rewrite ^(.*)\.stm$ $1 permanent; } }
At this point, I'm running out of options but to redirect every pages in Drupal manually ( that is roughly about 650 pages )
I've also tried every other modules, but non offers extension strip redirect.
My other option is using 404 landing page to run php and redirect any url with .stm and redirect with scrubbed url without .stm and stays in 404 if it's not a .stm url.
This is my best bet, URL rewriting with PHP
But it's over complicated just for a extension strip and redirect?

You should be able to check if the requested URI contains ".stm", and if so then redirect to the exact same URI with the ".stm" ripped. Put something like this in your settings.php:
if (strrpos(request_uri(), '.stm') !== FALSE) {
header("Location: " . str_replace('.stm', '', request_uri()));
exit;
}

Related

Redirection from the php file

I'm looking for a little help from someone who knows PHP,
in short, it looks like this:
I once made a website for a customer with products on the Jomla CMS system, and the product was located, for example, at:
www.mysite.pl/index.php/productall/product1
www.mysite.pl/index.php/productall/product2
e.t.c...
currently his site has been changed and is in the classic html5 / css / js without any CMS, but in the same old directory
and the product is on:
www.mysite.pl/product1.html
The client created QR codes for himself, but to the old page and printed on each product
and now I would like to create an index.php file, which will redirect the traffic depending on the call in the browser, because now as the QR code is being scanned it jumped out because there is no such adreas as before.
For now, I created an index.php file that redirects to
www.mysite.pl/allproduct.hml
where is the list of all products but I would prefer it to be as before
that is, each QRcode was redirected to the chosen product.
Can you do it somehow? PLEASE HELP
now i have this code in index.php
Instead of messing with PHP, You could try adding redirect commands within the htaccess file? (if you can still use one?) The ".htaccess" file should be located in the site root.
ie: /public_html/.htaccess
within this file you can create redirects from the old address to the new one.
Redirect 301 /oldProductLink1.html http://www.example.com/NewProductPage1.html
Redirect 301 /oldProductLink2.html http://www.example.com/NewProductPage2.html
:)
There's no easy solution you must create a .htacess file and place on your root folder then redirect all the old urls to new url for each product
Redirect 301 /index.php/productall/product1 www.mysite.pl/product1.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product2.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product3.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product4.html
If the question is only about PHP and there is no way to use web server URL rewriting or redirect, then you should create files like :
/index.php/productall/product1
/index.php/productall/product2
Where index.php would be a directory. Each file should contain something like :
header('location: /product1.html');
exit();
Where the product number should be set according to file name.
i still have this:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I removed this my test index.php file and created .htaccess in the root directory with yours codes

Rewriting the URL with htaccess (apache)

I have an website where in the homepage are displayed multiple articles. Every article has a link and when I click on it I pass the Id, the date and the title as parameters through the URL to "article.php" page. When I'm on the article page I recognize which article is by the Id and then I display the content.
But my problem is: when I open the "article.php" page my URL looks like this
http://127.0.0.1/Andrea/mySite/article.php?id=21&date=02%20march%202017%title=Basket%20NBA:%20Bulls-Warriors.%20Analysis
I have created the .htaccess file and I'm able to redirect people to other pages so the rewrite is enabled, what I'm searching is to change the URL from the above to something like this
http://127.0.0.1/Andrea/mySite/2017/03/02/basket-nba-bulls-warriors
So I want to remove the "Analysis" part after the point and "article.php" from the URL, the date to switch like if it was folders and the title to be written with scores between the words.
I have tried
RewriteEngine on
RewriteRule ^id/([A-Za-z0-9-]+)/?$ article.php?id=$1 [NC]
To remove "article.php" and add id between slashes but it doesn't seem to work.
Thanks in advice to everyone who will help me.
The .htaccess route with mod_rewrite
Add a file called .htaccess in your root folder, and add something like this:
RewriteEngine on
RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want, without the end user seeing it. Easy, but inflexible, so if you need more power:
The PHP route
Put the following in your .htaccess instead:
FallbackResource index.php
This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:
$path = ltrim($_SERVER['REQUEST_URI'], '/'); // Trim leading slash(es)
$elements = explode('/', $path); // Split path on slashes
if(empty($elements[0])) { // No path elements means home
ShowHomepage();
} else switch(array_shift($elements)) // Pop off first item and switch
{
case 'Some-text-goes-here':
ShowPicture($elements); // passes rest of parameters to internal function
break;
case 'more':
...
default:
header('HTTP/1.1 404 Not Found');
Show404Error();
}
This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.

Edit php script to open URL in file

I've just moved all my server and web apps over from apache to nginx.
I have one app which created short urls, it did this by create a 6 letter file and inside the file is simply the URL. I've managed to recreate the nginx rewrite rule so that it locates the file and opens its as php (instead of just downloading it as there is no .php extension on the files) however the script sees it as an invalid hash (as seen in the code below) is there anyway to simply this now just get my current links working, they all just contain a URL at the top, I want it to ignore the check and redirect to the URL irregardless.
Here is the script which is loading
<?php
if($_GET['id']){
$id = addslashes($_GET['id']);
if(file_exists("urls/$id"))
{
$url = file_get_contents("urls/$id");
header("location:".$url);
} else {
echo "Error : invalid hash";
}
} else {
echo "Error : no hash";
}
?>
and it loads a file calling something like, for example "0ke0ea9ts" which will contain a url and nothing else.
I'm allowing the original links created (eg url.com/p/0ke0ea9ts) to work with the following Nginx redirect code (note they're installed in the sub-directory of /p/ hence the reference)
if (!-e $request_filename){
rewrite ^(.*)$ /p/show.php?id=$1 last;
so now as I said, it redirects opens them via the show.php but gives me the invalid hash message, how can I force it to just run the URL without the check?

Move pages in Apache while kipping the same URLs

I want to rearrange my web site, and move all customer pages into a directory (/customerPages), while keeping the same URL (URL to access the page and the URL that is showed in the browser). I'm using Apache and PHP (CakePHP).
I've tried rewiring my 404 error page in the following way:
// Redirect customers to customers landing pages under /customerPages
if (strpos($message,'customerPages') == false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
$homepage = file_get_contents($url);
echo $homepage;
}
But this solution breaks all images written using relative paths.
Later I've tried using redirection instead:
if (strpos($message,'customerPages') == false) {
$url = 'http://'.$_SERVER['HTTP_HOST'].'/customerPages'.$message;
header("Location: ".$url);
}
But than the URL changes. I've tries fiddling with RewriteRule with no luck.
How can I achieve this using the first,second or any other method?
Another way, just basic idea:
1. Put in your /oldpath/.htaccess file (we handle file not found 404 error):
ErrorDocument 404 /oldpath/redirect.php
2. /oldpath/redirect.php file (our handler) - make redirect to new path only if file exists in newer location:
$url = parse_url($_SERVER['REQUEST_URI']);
$filename = basename($url['path']);
if(file_exists('/newpath/'.$filename)) {
header('Location: /newpath/'.$filename);
}else{
header('Location: /your_real_404_handler');
}
You need to redirect image requests from newer location (/customerPages) to the old path.
You can use mod_rewrite apache module to redirect such requests back:
RewriteEngine on
RewriteRule ^/customerPages/(.*\.jpg|.*\.gif|.*\.png) /oldpath/$1 [R]

PHP: prevent direct access to page

I have some pages that I don't want users to be able to access directly.
I have this function I came up with which works:
function prevent_direct_access()
{
if($_SERVER['REQUEST_URI'] == $_SERVER['PHP_SELF'])
{
//include_once('404.php');
header("Location: 404.php");
}
}
This does exactly what I want, the URL does not change but the content does. However I am wondering if there is something I need to add to tell search engines that this is a 404 and not to index it. keep in mind I do not want the URL to change though.
Thanks!
Don’t redirect but send the 404 status code:
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;
for the search engines, if you return HTTP status 404 they should not index I believe. But you could always redirect to somewhere covered by a robots.txt
Just to clarify:
You have some PHP that you want available to other PHP programs on the system
You do not want anybody accessing it except by running one of the other PHP programs
(i.e. "direct" doesn't mean "except by following a link from another page on this site")
Just keep the PHP file outside the webroot. That way it won't have a URL in the first place.
To ensure Search Engines don't index it, use a header() command to send a 404 lke this;
header("HTTP/1.0 404 Not Found");
Or put all such files in one folder, "includes" say, and add a "Deny /includes/" into your robots.txt file. This way, you can also add a ".htaccess" file in the same directory with one line - "Deny From All" - this will tell Apache to block access (if apache is configured properly), for another layer of security.

Categories