How can I show a page that not exists with PHP? - php

I have a website that show information about a shop online. I need following:
When you type example.com/shop/name-shop, this should show the name-shop page.
I'd like only have the file example.com/shop.php which show the information of shop based on the URL. I don't like use example.com/shop?name-shop.
Is this possible or I need to create one file to each shop? I tried to do this with an error page and a header location and works fine but in the url show example.com/shop instead of example.com/shop/name-shop.
Thanks.
EDIT:
I'm using the .htaccess in Apache with ErrorDocument 404 /error404.php In the file error404.php I've a if to check if url is http://example.com/shop/* When the url contains /shop/ redirect with header ("location: website") to the correct shop and works. But in the url in browser only show example.com/shop instead example.com/shop/name-shop. Obviously because i'm redirecting to shop.php

Depending on the web server you are using, you will need to use rewriting rules (mod_rewrite for Apache) or 404 pages to show custom content on each URL using only one PHP file.

I'd recommend you to use Symfony2. You can define routes with placeholders and you have a FrontController, which would correspond to your shop.php file.

Related

Wordpress Blog Post Redirect (1000 posts)

I have a wordpress blog with the following URL structure:
www.mysite.com/2016/09/the-name-of-the-post/
However i noticed that for SEO Reasons it would be better to have a structure like:
www.mysite.com/blog/the-name-of-the-post/
I have around 1000 posts with the "old" url structure. I can change the url structure in wordpress. However i would need a 301 redirect if someone tries to access a post using the old url.
E.g.
person/ google bot tries to access page via /2016/09/the-name
-> 301 Redirect to /blog/the-name
What is the best practice to do so? Do I add 1000 lines to the .htaccess file and do redirects? Would that negatively influence the server response time cause apache has to check through a long list of redirects first?
Can I tweak a file in the wordpress file structe which checks, if the requested page is included in e.g. an array, it will redirect it to the new url?
Thank you very much for your suggestions
You can simply change the URL structure in WordPress to the one you want to have. You don’t have to modify your .htaccess or anything else.
WordPress will understand which post is referenced by the URL and redirect to the new canonical URL automatically.
Just go to the Permalinks admin page and select the “Post name” option as described in the Codex.
However, if you’re introducing a prefix like /blog/, too, you must redirect to the new URL base through .htaccess, e.g.
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2)/(.+)$ http://example.com/blog/$1 // taken from stackoverflow.com/a/42211746/
You can use the following Redirect
RedirectMatch 301 ^/[0-9]{4}/[0-9]{2)/(.+)$ http://example.com/blog/$1
This will redirect all urls from this form /1234/12/foobar to this /blog/foobar
Change the permalink to the new structure and add this in your .htaccess
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)/$ http://example.com/blog/$3
I had the same problem before but I resolve this issue using simple 301 redirects plugin.
https://wordpress.org/plugins/simple-301-redirects/
You can bulk upload 301 redirects using bulk upload add-ons of this plugin.
Hope this will help you.

How to get the link that the user tried to visit, from a 404 custom page made in php

I have a website in different languages, thus I want to show a custom 404 page for each language.
Is there a way to find out which page the user tried to visit (that didn't exist) in order to find out which language I should display the 404 page?
The web server is Apache.
Thank you very much.
if you use .htaccess file for redirect then add
ErrorDocument 404 /yourdir/error/404page.php
then use request uri in session which stores the page user are visiting i.e.
$_SESSION['req_uri'] = $_SERVER['REQUEST_URI'];
then redirect user to $_SESSION['req_uri'].
where $_SESSION['req_uri'] is the page user trying to access.

404 Page is generated instead of proper 301 redirect to new post

I changed permalink from /%postname%-%post_id%/ to /%postname%/ only. But the problem is that I can't get any method to redirect 301 old permalink to new one.
I searched google and found this plugin which redirect the old permalink to new one. When I check the HTTP Response Header, it also shows the old page is being redirected to new one but actually in the content it shows 404 page. Here is the example page.
Even when I try to fetch the page in Google webmaster tools, the bot fails to fetch the old page.
Is there any way to fix the problem??
Quickest way is just add some redirect into your .htaccess file, if you use Apache server.
You can easily populate the url list for your legacy links by write some sql query.
.htaccess
# BEGIN FIX URL
Redirect /my-sample-post-100 /my-sample-post/
Redirect /my-2nd-sample-post-101 /my-2nd-sample-post/
...
etc
# END FIX URL

PHP, MySQL: Security concern; Page loads in a weird way

I am testing the security of my website. I am using the following URL to load a PHP page in my website, on localhost:
http://localhost/domain/user/index.php/apple.php
When I do this, the page is not loading normally; Instead the images, icons used in the page simply vanish/disappear from the page. Only text appears. And also on any link I click on this page, it brings me to this same page again without navigating to the required page. So if I have hyperlinks to other pages, such as "SEARCH", which points to search.php, instead of navigating to the search.php page, it refreshes the index.php page and just appends the page name of the destination page to the end of the URL.
For example, say I used the link above. It then loads the index.php page minus the images at it's will. When I click on the "Search" link to navigate to the search page, I see the following in the URL:
http://localhost/domain/user/index.php/search.php
I have a redirection configured to a 404 error page in my .htaccess file, but the page does not redirect to the 404 error page. Notice the search.php towards the end of the URL above. Any other link that I click, reloads the index.php page and just appends the destination page name to the end of the URL like I have shown above.
I was expecting to see a 404 Error but that does not happen. The URL should not even be able to load the page because I do NOT have a "index.php" folder in my website.
What can I do to solve this? All help is appreciated.
Update:
The security concern is that users being able to see a non-existing page (which is quite misleading) like http://localhost/domain/user/index.php/apple.php especially when it does not exists. This makes me feel that this is going to open doors for hackers to exploit the website and compromise its integrity. Can this happen in such a case? I want users to see a 404 error page and any I am willing to any change needed in .htaccess file to accommodate this.
Can you suggest me of some code that I can add to my .htaccess file to accomplish this?
Thank you.
EDIT1:
Here are the contents of my .htaccess files. I have 2 of them. One in domain root and the other in 'user' folder/directory.
/*.htaccess in domain root*/
ErrorDocument 404 /domain/404.php
/*.htaccess in user folder*/
ErrorDocument 404 /domain/user/404.php
EDIT2:
#Pekka Thanks for the link. I added the following code in the .htaccess file (within the user directory)
<Files "mypaths.php">
Options +Includes
SetOutputFilter INCLUDES
AcceptPathInfo Off
</Files>
But still this does not show me the 404 page. Sorry, I am very novice with the .htaccess. Hope you will be able to tell me what I am wrong. Thanks.
The behaviour of why this loads a page:
http://localhost/domain/user/index.php/apple.php
is easily explained. The request is passed to index.php, with apple.php being in the $_SERVER["PATH_INFO"] variable.
So you are in the /user directory as far as the server and the PHP script are concerned.
This is also why no 404 turns up: index.php is always found, no matter which file you specify as the last file.
The browser, however, interprets index.php not as a file, but as the parent directory of apple.php.
Therefore, every relative link you put on the page, say to contact.php is fetched like this:
http://localhost/domain/user/index.php/contact.php
which obviously won't work.
What you may want to do is use absolute paths in images and links, but either way, this is of no concern to security whatsoever.
As a side note, this whole phenomenon is sometimes used to create search engine friendly URLs without having to use mod_rewrite module.
You can turn this behaviour off using the AcceptPathInfo directive.
As for the images, you just have to learn to use absolute paths to your images, which is absolutely necessary
Just instead of images/head.jpg write /images/head.jpg and you will have all your images and styles in place.
As for the /user/index.php/apple.php - why do you want such an odd address?
Why not to use just user/apple.php?
And where is security in your question?

How to redirect old "ugly" urls to seo-friendly ones?

I'm new to mod_rewrite and need to do something for my client.
Suppose I have the www.mydomain.com/products.php?prod_id=32.
This product has a section (clothes) and a name (shirt). These names and sections are unique.
In a SEO-Friendly Url, it should be www.mydomain.com/products/clothes/shirt/.
I know I can create
RewriteRule ^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ products.php?section=$1&name=$2
I can do that, and it works. But I want people who enter www.mydomain.com/products.php?prod_id=32 to be redirected to www.mydomain.com/products/clothes/shirt/ (changed in the browser itself). How can I do that without inserting the id in my url? Is it possible to call a "pre-processing" php file in my .htaccess, and recreate "products.php?section=$1&name=$2"?
Anyone has a good link with really detailed explanation of mod_rewrite?
Thanks!
You may have a bigger problem than mod_rewrite can handle gracefully. You can also use PHP to return a redirect to the browser. That way you can access your database to figure out that product_id 32 is /clothes/shirts/
I see no other option than doing it inside PHP.
You can add something to the top of your products.php page that checks the URL ($_SERVER['REQUEST_URI']) to see if it contains products.php - If it does, redirect the person. You'll need to query your database to find out the product category and the name before redirecting though.
Remember to set the Moved Permanently header to improve SEO further :)
This data might be passed by the browser via the "Referer" header field. You could progress that url and look at the get arguments. If I remember right, this isn't supported on all browsers.
at the top of products.php (or any page you want to redirect) you could put a function call
redirectToSeoUrl();
Then in one of your include files write a redirectToSeoUrl function that gets the new url and redirects. Make sure to put the code before anything is output to the browser.
Apache? Try an .htaccess redirect. No mod_rewrite needed.
Example:
Redirect 301 /oldpage.html http://www.example.com/newpage.html

Categories