My file structure looks like this:
/
index.php
htaccess
cabinet
public
file.txt
index.php
I want to have a redirect from cabinet/file.txt to cabinet/public/file.txt,
and if cabinet/public/$filename does not exists then to redirect to cabinet/public/index.php instead.
How can I do this properly?
You can use a rewrite rule in Apache. Here is a good place to start if you are not familiar. What you want looks something like this:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^cabinet/file.txt cabinet/public/file.txt
ErrorDocument 404 cabinet/public/index.php
Keep in mind that the ErrorDocument will be used for all 404 or files not found.
You can use this .htaccess in /cabinet/ folder:
RewriteEngine On
RewriteBase /cabinet/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/cabinet/public/$1 -f [NC]
RewriteRule ^(.+?)/?$ public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!public/).*)$ public/index.php [L,NC]
Related
I have the file path: example.com/blog/ where blog is a directory. Blog gets a query string from the URL called URL and makes a dynamic page out of that information. So essentially it's something like this: example.com/blog?url=hello-world but i'd like to remove the ?url part and instead add a slash. So it should look something like this: example.com/blog/hello-world I've tried to accomplish this by putting the .htaccess file in the blog directory. This is my current .htaccess file, but it is not working:
.htaccess
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1 [L,QSA]
Inside /blog there is a index.php file and that generates the dynamic page.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^blog/([A-Za-z0-9-]+)/?$ blog/index.php?url=$1 [NC,L]
RewriteRule ^blog/?$ blog/index.php [NC,L]
Try this one.
EDIT
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(blog)/(.*)$ blog/index.php?url=$1
I am trying to use mod_rewrite to clean up the $_GET[] info from the URL
Currently have this as my .htaccess
# BEGIN Rewrite
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# END Rewrite
I have and about.php page that has a few sub pages and with that .htaccess file it looks like
http://domain.com/about
to view the subpage "mission" It would look like
http://domain.com/about?subpage=mission
What is the best way to clean this URL up? I suck at the .htaccess stuff.
EDIT
Edited to show a generic rule:
RewriteCond %{THE_REQUEST} ^GET\ /([^?]+)\?subpage=([^&\s]+) [NC]
RewriteRule ^.+$ /%1/%2? [R=301,L]
RewriteRule ^(.+)/([^/]+)/?$ /$1.php?subpage=$2 [L]
With the following .htaccess file, I obviously, get alot of 404's on loading scripts and css because the browser keeps looking in the wrong directory.
I am a newb at htaccess and have no clue about how to fix it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ client/app/index\.html [L]
</IfModule>
Dir structure is as follows:
project /client / app / index.html
.htaccess is located in project directory.
I recomend you that redirect users with a 301 to correct path. This will end with your problems.
RewriteRule ^$ client/app/index\.html [L, 301]
If you are worried about seo look at https://support.google.com/webmasters/answer/93633?hl=en
All of the calls at your server are being redirected to your RewriteRule. Add a RewriteCond (RewriteCondition) to ignore CSS and JS files, if they exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^$ client/app/index\.html [L]
Explanation:
RewriteCond %{REQUEST_FILENAME} !-f
If the requested filename exists (css/main.css), do not go through with the RewriteRules.
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
If the file does not exist, but matches one of the extensions, do nothing. This is helpful because you get a 404 if css/main-2.css does not exist, rather than redirecting you to index.php.
And then finally is your own rule, which redirects all non-conditioned rules.
In your .htaccess file, this is what worked for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^ /index.html [R=301]
Thank you!
My existing .htaccess file looks like this:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^(.+/)?posts/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
RewriteRule ^(.+/)?aposts/(.*)/(.*).html$ index.php?content=autopost&postid=$2&kwd=$3
RewriteRule ^(.+/)?category/(.*)/(.*).html$ index.php?content=category&cat=$2&otext=$3
RewriteRule ^(.+/)?content/(.*).html$ index.php?content=$2
RewriteRule ^(.+/)?searching/(.*).html$ index.php?content=search&k=$2
RewriteRule ^(.+/)?related/(.*).html$ index.php?content=related&k=$2
RewriteRule ^(.+/)?blog/(.*)/(.*).html$ index.php?content=blog&postid=$2&kwd=$3
RewriteRule ^(.+/)?posts$ index.php?content=blog
RewriteRule ^(.+/)?sitemap\.xml$ sitemap.php [L]
RewriteRule ^(.+/)?robots\.txt$ robots.php [L]
ErrorDocument 404 /404.php
I would like to add the ability for all subdirectories to use the root index.php file. For example, if a visitor goes to the root directory:
example.com
They get served the root index.php file.
I want it so that if anybody goes to any other subdirectories eg:
example.com/sub1
example.com/sub1/sub2/sub3/etc..
They get served the index.php file from the root directory BUT the browser address bar still shows the subdirectories, eg:
example.com/sub1
example.com/sub1/sub2/sub3/etc..
Being shown how to add exceptions to this rule would be great too.
Thanks for any help.
Right under RewriteBase / add:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 !^index\.php
RewriteRule ^(.+)$ /index.php [L]
To add exceptions, just add more RewriteCond lines. For example, to make it so /foo/ doesn't get routed to index.php, add:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 !^foo/
RewriteCond $1 !^index\.php
RewriteRule ^(.+)$ /index.php [L]
I have a website that uses mod_rewrite for pretty urls. I have two main urls:
example.com/id
example.com/generate/id
And this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^generate/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]
RewriteRule ^([a-z0-9_-]+)$ contact.php?id=$1 [L,NC]
The first url is working correctly, but the second not. Apache shows a 404 error, I think that it's because it's looking for the folder "generate", that doesn't exist, and it can't find the htaccess in the document root.
Thank you
Have you try
RewriteRule ^generate\/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]