I want that domain.tld/en/ is a 100% mirror for domain.tld/ with all its (sub-)directories and files but does not change its URL for the client. Currently the PHP files can't access all their needed files (like CSS) because they can't find them in domain.tld/en/css/file.css (just in /css/file.css which should be the same)
This is what my .htaccess looks like right now (adapted from another question (see below)):
RewriteEngine On
RewriteRule /en/(.*) /$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
Background
I can't find a solution for my problem on stackoverflow.
Its very near to this question Redirect from one directory to another with mod_rewrite, but because I don't really understand how to modify htaccess files it doesn't help me, sadly.
I have a website running in the index.php of a main directory e.g. domain.tld/index.php, now I have modified the php file with replaceable strings for multilingual content and a detection in which directory its used in.
There should not be any difference between files in the root directory and files in other directories. try this one (note that I've added L flag which will make the mod_rewrite stop looking for other rules when the first one was matched (in your case any file under en/ should have match both the first and the second rules - so it would apply both rewrites
RewriteEngine On
RewriteRule ^\/?en\/(.*) $1?lang=en [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
Related
Not sure if I titled my question accurately. I have a directory structure like this:
example.com/edit/43424242/
Where the number at the end is not actually a folder. It's really just a variable that I want to use at the /edit/index.php directory.
I tried using this in my .htaccess file at the root directory:
RewriteEngine ON
RewriteRule %{QUERY_STRING} ^$
RewriteRule ^edit/(.*)/?$ edit/index.php?var=$1 [NC,L]
But that didn't work because it messes up my AJAX post to another file in that directory /edit/post.php
So I thought maybe I need to modify my .htaccess file so that I can still retrieve the subdirectories as a variable and still be able to use post.php or perhaps modify .htaccess so that it allows index.php to communicate with post.php.
Any ideas?
RewriteRule %{QUERY_STRING} ^$
It is not enough for check for empty query string as it will not prevent this rule affecting serving real files and directories.
You may use this rule:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^edit/([\w-]+)/?$ edit/index.php?var=$1 [NC,L,QSA]
Well, as a start please excuse me for my beginner English..
I want to know more about security in PHP MVC applications
I've created my own MVC, I still haven't finished it.
My application directory is exposed by URL access with child elements.
How to make this hidden from visitors?
Following is what I am trying
Apache mod_rewrite ?
I still don't know to make it empty index.html in each folder like the framework Codeigniter ?
What to use for something to indicate ?
and,
... how to make ?
Edit
I know a litte something about rewrite_rules
Below is my .htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /ligia
#RewriteCond %{REQUEST_FILENAME} -f [OR]
#RewriteCond %{REQUEST_FILENAME} -l [OR]
#RewriteCond %{REQUEST_FILENAME} -d
#RewriteRule .+ -
#I know, it is commented
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule "^(.+)$" "index.php?uri=$1" [QSA,L]
But I am afraid if this is the best way to hold my MVC application
security!?
I need help!
First make sure that your .htaccess file is in your document root (the same place as index.php) or it'll only affect the sub-folder it's in (and any sub-folders within that - recursively).
Next make a slight change to your rule so it looks something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
At the moment you're just matching on . which is one instance of any character, you need at least .* to match any number of instances of any character.
If you want the whole shebang installed in a sub-directory, such as /mvc/ or /framework/ the least complicated way to do it is to change the rewrite rule slightly to take that into account.
RewriteRule ^(.*)$ /mvc/index.php?path=$1 [NC,L,QSA]
And ensure that your index.php is in that folder whilst the .htaccess file is in the document root.
NC = No Case (not case sensitive, not really necessary since there are no characters in the pattern)
L = Last (it'll stop rewriting at after this Rewrite so make sure it's the last thing in your list of rewrites)
QSA = Query String Apend, just in case you've got something like ?like=penguins on the end which you want to keep and pass to index.php.
So I'm using the Flight PHP microframework (http://flightphp.com/) to do routing. My question is, how can I run the router from within a subdirectory? What I mean is, essentially, run it 'sandboxed' within a folder.
As in, a request to '/' just pulls the regular index.php file. But a request to '/flight/file' would load the URL using Flight.
I know you can't just dump it in a folder on the server and expect it to work because FlightPHP expects the URLs relative to the root.
Is there a way to run FlightPHP isolated in a directory with the rest of the website running regular PHP?
EDIT
I tried simply putting the .htaccess file into the subdirectory. This has the peculiar effect of causing the routes to still act as if they are from the root (e.g. /thing/otherthing/ when it should be /otherdirectory/thing/otherthing/ ) while simultaneously causing the 404 callback to not work. Not what I intended.
EDIT 2
Contents of .htaccess file, which are what is suggested by the Flightphp website:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
I know this is an old question but I've been doing something similar just by adding something along the lines of
RewriteBase /flight/
in the .htaccess file (before all of your rules) of the flight directory. Hope this helps someone else looking for the same thing. Example:
RewriteEngine On
RewriteBase /flight/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Sorry for giving you an answer that is not going to help a lot. I'm running Flight (also sandboxing) on a subdirectory. I've created an .htaccess just with the defaults in the subdir and Flight is now regarding this as it's root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Did you check the content of the .htaccess on the higher levels, maybe there is something that is blocking you
I have seen in Wordpress, when you create a page say "Register", it points to http://example.com/register/ but without having a folder named register in the root at all. How is this done?
Its like this. When a user clicks the link on http://example.com/index.php which says Register, it takes the user to a new page and the URL in the browser would be http://example.com/register/ and the page loads. The register folder itself does not exist.
From the answers below I learnt that the request is passed through the index.php by modifying the .htaccess
I want to know what code to place in index.php so that http://example.com/register/ would display the register page.
In my Wordpress .htaccess file I have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Which basically says if the "filename" part of the URL doesn't exist as a file !-f or exist as a directory !-d then rewrite the request internally to be /index.php which forces the request to be processed by index.php of Wordpress.
Ok, based on your additional comments, the following .htaccess rules will take http://domain.com/<anything> and internally rewrite it to http://domain.com/index.php?page=<anything>, this will be done without regard to case (NC flag) and will keep (pre-pend) any existing query string (QSA flag). This only does this for filenames and directories that do not exist on the server.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [NC,QSA,L]
This does have the following side effect which you should be able to handle programmatically, http://domain.com/register/ will be rewritten to http://domain.com/index.php?page=register/ which includes the trailing "/" since this is a directory reference. Again, you should be able to handle that in your PHP code.
When the web server, Apache in this case, attempts to open a requested file path, /register/ in this case, it first looks at the .htaccess file.
The .htaccess file among many other things can redirect the request to another path.
What usually happens is all requests redirect to a single file, index.php in WordPress, which reads the original request, /register/, and serves up the correct html.
What WordPress does to serve up the correct html is another question altogether but you can start here: https://www.google.com/search?q=how+wordpress+works
Wordpress uses mod_rewrite in .htaccess file as follows:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Which routes all page requests through index.php if the file or directory doesn't exist and the script is not index.php
Neil Martin, this is really not a simple question. You are better off reading some good material on URL REWRITING
Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.
For example: "/users/username1/" and "/users/username2/"
If some one wants to access the subdirectory they would simple go to:
"www.example.com/users/username1/" or "www.example.com/users/username2/"
The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.
I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??
If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.
Thank you
Well, for example, you could do it all with one htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
What it does:
switches on rewrite engine
checks if a requested file exists
checks if a requested directory exists
if NOT, it redirects request to your main index.php
Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:
index.php?url=/users/ivan
then you $_GET['url'] in your index.php and split it into pieces.
That's just an example, there other mod_rewrite methods to do this.
Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.
With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.
By using something like this:
RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
You can customize RewriteRule as much as you want.
You can essentially type in any directory you want, and it will be redirected to your index.php page.
If you want to make sure the existing directories are not redirected, do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ users.php?a=$1 [L]
This is a very similar setup I use to distribute CSS files.
Note: The Directory will be contained is $_GET['a']