htaccess to rewrite the original URL - php

I am creating SEO friendly URLs for my website. I have a file notfound.php to which I am redirecting all the URLs. Now, I want notfound.php to search for the URL in database and generate the corresponding content.
But the problem is that in the URL it shows is http://some_url/notfound.php
I don't want this URL to be displayed in the address bar, instead I want the original one.
For example,
http://some_url/hello/world is redirecting to http://some_url/notfound.php using ErrorDocument 404
and notfound.php is serving the content
But how to show http://some_url/hello/world in the URL instead of notfound.php? And, is this the right way to redirect and rewrite while matching the URLs from a database?

you must have mod_rewrite enabled, then you can try:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ notfound.php [L]
the first two lines ensure that you can still load things that exist (like js and css files that you may need)

ErrorDocument does an external redirect when you give it a complete URL. The doc tells you how to do an internal redirect.
http://httpd.apache.org/docs/current/custom-error.html#configuration

Related

How to redirect a url with .php to 404

I'm working on a php project and I have successfully removed .php extension from the URL using .htaccess. But I can easily access the url by typing .php too. Is there any way to redirect to 404 when there is .php in url
This is how my .htaccess looks like -
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
ErrorDocument 404 /page-not-found.html
Thankyou for your help
Hmm..but you are redirecting to *.php and if you add another redirection from *.php to 404 page you may end up redirecting all your pages ( without php -> with php -> 404) to 404 page.
Common practice is to redirect all "SEO" urls (those without .php extension) to a single php file (i.e. index.php). Then you can redirect all other .php files to 404 page with exception of that index.php.
This solution is good because in index.php you can do some common stuff, needed for all pages (i.e. connect to database, check authentication or something) and then from index.php include appropriate file to generate page content.
Change to your file .htaccss should look like:
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ index.php?page=$1
and then your index.php will get page name inside get $page parameter so you can include appropriate file..
--
For excluding index.php from redirection you should add extra rewrite condition, which should look something like:
RewriteCond %{REQUEST_URI} !^/index.php
.. meaning, request uri must not be index.php for this redirection to work. Place this with other conditiones, above redirect rule of course.
You have online htaccess tester tools. Try it for your self.

PHP how to redirect url path to index.php

I currently have a website with URLs with this pattern
https://website.com/?p=something
where p is the requested page (like /?p=login)
for SEO reasons i want the URL to look like this
https://website.com/login/
Of course I could do a redirect like this
Redirect 301 /login/ https://website.com/?p=login
But it would change the URL displayed in the users browser back to the URL with parameters in it.
My question is, how can I have an URL like this https://website.com/login/ but in PHP inside the index.php I could access the requestet page or "path" like before like $page = $_GET['p'];. In other words I want to redirect the URL https://website.com/login/ to https://website.com/?p=login but having the URL still displayed as https://website.com/login/.
I'd suggest to use .htaccess file to point all requests to the server to one file for example index.php
# Rewrite Engine ON
RewriteEngine on
# Redirect everything to one file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
After that you can code a router to handle URI components.
For example someone call sitename/login
You detect /login and open the required page

Capturing and preserving URI on redirect on Apache php Server without 404 header

Capturing and preserving URI on redirect on Apache php Server without 404 header
I'm building a news website/blog. I want to format the URL in a specific way. Like the following:
    http://example.com/news/1234/title_of_article/
I don't want the hassle and mess of creating a directory structure for every article. I've set my .htaccess file to redirect to a main file with
     ErrorDocument 404 /main_file.php
My main file then uses $_SERVER[REQUEST_URI] to find out what page was requested, and then build the the page.
It all works well. The only problem, I believe the .htaccess redirect sends a 404 header to the client. I've tried to counteract this with a follow up 200 header in the main file, but I don't think it works. W3C Validator detects the 404 still and search engines don't seem to index the pages. 
How can I keep the format of the URL without sending a 404 error to the browser? Is there an easy alternative without making dozens of folders?
You could use a simple internal redirect to redirect any file or folder that does not exist to your main_file.php instead of using the ErrorDocument for doing it:
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# if file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# redirect anything to
RewriteRule ^(.*)$ main_file.php/$1 [L]
And handle it using $_SERVER['REQUEST_URI'] or you can further customize it to handle it as parameters like this:
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# if file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# redirect anything to
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ main_file.php?category=$1&id=$2&title=$3 [L]
The above would internally redirect:
http://example.com/news/1234/title_of_article/
To:
http://example.com/main_file.php?category=news&id=1234&title=title_of_article
However the browser URL would remain:
http://example.com/news/1234/title_of_article/

htaccess need help rewriting url

I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.

Redirect direct image urls requests and pass the original url value in the redirection

Suppose I have someone (or Google Images) trying to access
http://null.com/uploads/someimage.jpg (actually any image in /uploads folder)
I'd want htaccess to redirect the user to
http://null.com/page/
HOWEVER, I need to pass the original url (http://null.com/uploads/someimage.jpg) to the redirection target page as a POST string value which the target php page can use to do stuff with it
Would it be possible to achieve that with htaccess?
UPDATE - and also, it would be interesting to have this working only when the user agent is a human operated browser, not a bot such as google bot, in this way Google and other search engines can crawl images appropriately without the redirection
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteCond %{REQUEST_URI} ^/uploads/([^/]+)/? [NC]
RewriteRule . page.php?url=uploads/%1 [L]
Maps silently
http://null.com/uploads/someimage.jpg with or without trailing slash
To:
http://null.com/page.php?url=uploads/someimage.jpg
The string uploads is assumed to be fixed, while someimage.jpg can be any name.
The script name page.php is an example an can be any name also. Replace all instances in the rule-set.
For permanent and visible redirection, replace [L] with [R=301,L]
This answer is according to this description in OP comments:
"...redirect the user to this page and pass the former original url as a post value..."
where page is a script.
NOTE: The parameters passed in the substitution URL can be captured with PHP using $_GET or $_REQUEST, not $_POST.
UPDATE
Here is another version:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php [NC]
RewriteRule ^(.*)\.jpg page.php?url=$1.jpg [L]
Maps silently
http://null.com/any/number/of/folders/someimage.jpg
To:
http://null.com/page.php?url=any/number/of/folders/someimage.jpg
The rewrite rule is applied only when the incoming URL holds a file with extension jpg at the end of the path.

Categories