Denying main links with .htaccess or anything else - php

I'm using an encrypted php program with smarty template.
I wanted to open links such as index.php?page=login to open with login.php, so I used Rewrite mode in .htaccesss, for example
RewriteRule ^/*login.php$ /index.php?page=login [L]
in a few pages there should be a query for errors such as
RewriteRule ^/*support.php?(.+)$ /index.php?page=support&%{QUERY_STRING} [L]
It works but I need to deny the main URLs so when users enters index.php?a=login manually nothing (or the main page) would show up and he should open the link only with /login.php.
Is there a way to do this?
A little help please...

It should work with a coirresponding rewirterule before(!) the other rules:
RewriteRule ^/*index.php?a=login$ / [L]

Related

htaccess Working fine for all but few links not working

For my work portfolio i am using htaccess for seo friendly URL and its working fine for all but for two projects its not working and going to another page (rs.php) and redirecting to home page.
Site is https://www.rsseosolution.com/seo-work-portfolio.php
At this page all projects are opening fine but Number 4 (Fair Price Movers) and number 11 (New Hampshire Lawyers) ..... these two are not opening while URL and process is same.
page name is case-studies.php
I tried by add "echo and die option" at starting of page
echo "Testing value is coming on not"; die();
But as i said that its going to another page rs.php
Using htaccess as below for our portfolio project details at case-studies.php
Options +FollowSymLinks
RewriteEngine on
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2
RewriteRule package/(.*)-(.*)\.php$ package.php?name=$1&id=$2
RewriteRule rs-(.*)\.php$ rs.php?cid=$1
I am not getting that why only two projects URL is going to rs.php.
What am i missing? If need any other details then please let me know.
Hope for quick response.
The error happen in this url fair-price-movers-164 as it contain rs-164 which will match the last rule.
You need to add [L] at the end of each rule to stop matching.
Options +FollowSymLinks
RewriteEngine on
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [L]
RewriteRule package/(.*)-(.*)\.php$ package.php?name=$1&id=$2 [L]
RewriteRule rs-(.*)\.php$ rs.php?cid=$1 [L]

How to hide folders from URL using php or htaccess

I have images and documents located after a series of folders like this:
http://domain.tld/library/data/info/history-of-america/hoa1.pdf
http://domain.tld/library/data/info/history-of-america/hoa2.pdf
http://domain.tld/library/data/info/50-moments-in-history/50mih.png
I have used php to redirect shorter links to these long URLs like so:
http://domain.tld/15
http://domain.tld/16
http://domain.tld/21
But because I am using the header redirect, when I click these short links they redirect to the files and display the long filepaths again.
How can I preserve the short links or at least get rid of the folders in the long links when the file is visible on the browser?
http://domain.tld/15
http://domain.tld/16
http://domain.tld/21
OR
http://domain.tld/hoa1.pdf
http://domain.tld/hoa2.pdf
http://domain.tld/50mih.png
Note I have hundreds of these files so manually inputting for each one is not scalable for me.
Thanks in advance. I've just been getting so much errors when trying other Q&A solutions so a clear path would be much appreciated.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^15/?$ library/data/info/history-of-america/hoa1.pdf [L,NC]
RewriteRule ^16/?$ library/data/info/history-of-america/hoa2.pdf [L,NC]
RewriteRule ^21/?$ library/data/info/50-moments-in-history/50mih.png [L,NC]
Include this in your .htaccess page
#url forwarded
RewriteRule ^([a-z0-9_.-]+)$ library/data/info/history-of-america/hoa1.pdf$1 [L,NC,QSA]

.htaccess create user friendly url and ridirect to it

i tryed to search everywhere for this problem but i didnt found nothing.
I want to make make a url seo friendly so i used this code:
RewriteEngine on
RewriteRule ^Homepage index.php [NC,L]
Then i want to redirect to it so i tryed to write this code:
RewriteRule ^index.php$ http://localhost/siti/socialmark/Homepage [R=301,L]
The error it's a loop of redirections, can someone help me?
SORRY FOR MY BAD ENGLISH!
The rewrite rules don't just make the URL string look different, it actually directs the user to the file at the end of the path even if you don't see it in the address bar. If Homepage is a directory containing index.php, even if that php file name doesn't appear in the URL, then it's causing a loop because it's directing you to a directory with an index.php.
The rule is executed every time that page loads. So, you're redirecting to a page which runs the redirect script, so it runs the rule to redirect again, and that causes the loop. What you want to do is create a condition that says "Don't run this code if the requested page is http://localhost/siti/socialmark/Homepage"
Something like this (you may have to adjust it)
RewriteBase /
RewriteCond %{REQUEST_URI} !=/siti/socialmark/Homepage
RewriteRule ^Homepage index.php [NC,L]
For more details, see the caveats and example here:
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l

Can't access post or get from a php page after redirecting

I'am redirecting about 100 hmtl pages to a single PHP page (example.php) using .htaccess. It is working perfectly.
I've pagination on that page (example.php) but I am using the original HTML page URL (example.html?page=2&limit=20)
so example.html, example1.html, example2.html, example3.html are all redirecting to example.php.
The address bar is still showing ".html" URL but due to .htaccess redirection the example.php is rendering.
when is click on a pagination link (example.html?page=2&limit=20) the browser address bar shows correct .html URL and query string.
I've tried to get the values of page, and limit using $_GET and $_REQUEST in (example.php) but i am not successful.
Please help me in reading the (example.html?page=2&limit=20) query string parameeters .
Edit Code ported from comments:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L]
Add the QSA flag, which means "query-string append" to be sure the existing query string is ported into the rewritten URL.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.*)$
RewriteRule ^page-(.*)$ size-content.php?sef=$1 [L,QSA]
.htaccess modify your server configuration.
if you are making redirection then you change your request.
Try mod_rewrite if you are using Apache of course.
RewriteEngine On
RewriteRule ^example\.html\?(.*) example.php?$1
Mod rewrite is module to Apache. It is not allowed on most free hostings.
Yasir - you can resolve this problem by two ways:
1) Re-write rules for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^page-(.).html(.)/(.)$
RewriteRule ^page-(.).html(.)/(.)$ size-content.php?sef=$1&page=$2&limit=$3 [L]
This rule will handle: page-example.html?page=2&limit=20
I hope - you will easily understand the above rule.
Note: Keep one thing in your mind that every link should be in same pattern if you change rule in htaccess.
2) You can resolve this problem on your "size-content.php"
Suppose page-example.html?page=2&limit=20
$_GET['sef'] = example.html?page=2&limit=20 [according to you .htaccess]
Now you can parse this string via explode function
Thanks

how can i get my htaccess to make my url look like foo.bar/user with this snippet i have

I have been trying to get my urls to be more user friendly and I have come up with this set up
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage?user=$1 [NC,L]
I added this to my .htaccess but I'm now i'm confused as to how to access these urls.
in my index.php when a user logs in i have tried to redirect the user using
userpage.php?user=s2xi
but the url parses as www.foo.bar/userpage.php?user=s2xi and not www.foo.bar/s2xi
and also tried this as a check to see if user exists (is there a better way?)
if($_GET['user'] != $_SESSION['username']){
header("Location: no_user.php");
}else{
//load page
}
I am using the Smarty template engine on my site and I have my 'themes' in directories that belong to members file
www.foo.bar/users/s2xi/themes
but i want www.foo.bar/s2xi to point to the persons profile page that is viewable by everyone else and not their accounts page.
You're missing the .php in your RewriteRule, if that's verbatim - eg, userpage? => userpage.php?.
However, you're going to run into some problems with this unless you're using a framework to help you distinguish between routes. If you switched to using a separate URI format for user pages (eg /user/foo) you wouldn't have conflicts; but as it stands currently, using .htaccess to rewrite your URLs in that format could potentially cause problems with many other parts of your app.
To do it with a separate URI format, change your last .htaccess line (the RewriteRule) to:
RewriteRule ^user/(.+)/?$ userpage.php?user=$1 [NC,L]
may want to consider adding QSA as well.

Categories