RewriteRule on alias url - php

on query:
domain.com/page/do
i need:
domain.com/index.php/page/do
etc.
i use htaccess file, i add alias
RewriteBase /index.php/
How i write RewriteRule to this url?

Try this, it should do what you want, and eliminate the need to have index.php in your uri
###
# Rewrite Rules
#
<IfModule mod_rewrite.c>
### Enable mod_rewrite
RewriteEngine On
RewriteBase /
### Checks if the URI request path coincides with a literal path
### on the filesystem relative to the webroot. If it does not exist,
### then it passes the path through index.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Try this:
RewriteRule ^(.*)/(.*)$ /$1/$2 [L]

Add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteBase /
RewriteCond ^/index.php
RewriteRule ^/?(.*)$ /index.php/$1 [L]

Related

Laravel .htaccess subfolder

I now have a Laravel installation with the default folder structure.
In my root folder I have a .htaccess file like this one.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^ index.php [L]
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I have to install in a subfolder WordPress.
So I would like to access to my WP installation using the URL http://www.example.com/ and to Laravel with http://www.example.com/admin.
The thing I know it's that is an .htaccess trick, but, even if I tried many kind of rewrites, I'm not still able to achieve the desired result.
So, in your root .htaccess you should use the following:
RewriteEngine On
# Route any request begining with 'admin/' to laravel
RewriteRule ^admin/(.*)$ public/$1 [L]
# Anything else goes to Wordpress
RewriteRule ^(.*)$ wp/$1
And in your Laravel .htaccess add:
RewriteBase /admin
If you can't live with the trailing slash being mandatory you can try:
RewriteRule ^admin(/(.*))?$ public/$2 [L]
Try this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /your-subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /your-subdirectory/index.php [L]
</IfModule>
# END WordPress

rewrite all the requests to default.php except the request for sitemap.xml

I am using htaccess to rewrite every request to default.php file.
Options -Indexes
RewriteEngine On
RewriteRule ^ blog/default.php
But I want to submit sitemap file of my website(sitemap.xml).So that request to http://example.com/sitemap.xml rewrites to -> blog/sitemap.xml.
My unsuccessful try->
Options -Indexes
RewriteEngine On
RewriteRule ^sitemap.xml blog/sitemap.xml
RewriteRule ^ blog/default.php
My htaccess rules are obviously not hardened. But I am new to this.Please help.
I think you can use RewriteBase to reduce path to the blog directory. Try something like:
RewriteEngine On # enable rewrite engine
RewriteBase /blog/ # Set path "./blog" as a root
RewriteCond %{REQUEST_FILENAME} !-f # Excludes existed files from rewrite
RewriteCond $1 !^(default\.php|sitemap\.xml) # Setup $1 variable
RewriteRule ^(.*)$ default.php/$1 [L] # Process request params to default.php
The rules are fine so far, although I guess, you get a rewrite loop. To avoid the loop, you can prefix the rules with a rewrite condition, which skips the rule when the requesst is for a real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap.xml$ /blog/sitemap.xml [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /blog/default.php [L]

Change root path from .htaccess

I would like to change "/" to "/folder".
So when user clicks link with this url "/page.php" I want it to be automatically translated by .htaccess to "/folder/page.php".
How can I achieve this?
This simple rule should work for you in site root .htaccess (parent directory of folder):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/ [NC]
RewriteRule .* folder/$0 [L]
Please use .htaccess file. i think its better for you.
Your Need is:
HTTP://YOUR_DOMAIN.COM/page.php
TO
HTTP://YOUR_DOMAIN.COM/FOLDER/page.php
Step1:
create an .htaccess file on your Server
Filename: .htaccess
just Copy this part and Change the Values.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} YOUR_DOMAIN.COM$ [NC]
RewriteCond %{HTTP_HOST} !FOLDER
RewriteRule ^(.*)$ http://YOUR_DOMAIN.COM/FOLDER/$1 [R=301,L]
Reference-1 |
Reference-2
Try this:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [QSA,PT,L]
RewriteRule ^$ /folder/$1 [QSA,PT,L]
RewriteRule ^index.php/(.*) $1 [QSA,R,L]
</IfModule>

How to configure apache to allow rewrite rules for directories?

I have some configuration in htaccess:
RewriteEngine On
RewriteRule ^(.*)$ /index.php [L,NC,QSA]
All urls redirects to /index.php
But if folder exists, it goes to folder, not to /index.php
why?
I tried this solution to make this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . index.php [QSA,L]
But this like a previously example goes to directory =(
Try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
Or
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule (.*) index.php [QSA]
Use something like this:
#turn rewrite engine on
RewriteEngine On
#specify your base
RewriteBase /
#make sure, your index.php won't be rewritten
RewriteRule ^index.php - [L]
#rewrite everything else
RewriteRule ^(.*)$ index.php?para=$1 [QSA,L]
But if folder exists, it goes to folder, not to /index.php
Try disabling MultiViews – it is often interfering in such cases.
Options -MultiViews
http://httpd.apache.org/docs/2.2/mod/core.html#options
http://httpd.apache.org/docs/2.2/mod/mod_negotiation.html#multiviews

.htaccess rewrite rule with static files gives me 404, what I'm missing?

My project (Silex) has a front controller web/index.php and assets in web/css/*, web/img/* and so on. I've placed the following .htaccess file in the root of the public html folder:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Every request should be rewrited to web folder (RewriteBase web directive) and if file doesn't exist (!-f) it should be routed to the front controller.
This is not working:
<img src="/img/myimage.png" alt="" />
File myimage.png exists, but request for /img/myimage.png gives me a 404 folder. If I modify the path to /web/img/mmyimage.png it works.
Is there something I'm missing?
You could try routing the css and img folders explicitly:
RewriteCond %{DOCUMENT_ROOT}/web%{REQUEST_URI} -f
RewriteRule ^(css|img) /web%{REQUEST_URI} [L]
You'd need to add that right before the condition for the index.php routing rule.
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
# if requested files exists in /web then forward it there
RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
RewriteRule ^(.+?)/?$ /web/$1 [L]
# if requested files doesn't exist in /web then forward it to index.php
RewriteCond %{DOCUMENT_ROOT}/web/$1 !-f
RewriteRule ^(.+?)/?$ /web/index.php [L]
if your server is running Apache >= 2.2.16 you can use:
FallbackResource /index.php
in your .htaccess instead of
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
See also Apache’s fallbackresource: your new .htaccess command for more information.
You need to change web to /web as Kyra mentioned.
Options -MultiViews
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
But that's not going to solve your issue.
Main problem is your DocumentRoot is pointing to /path/to/project and it should point to /path/to/project/web
I managed to fix it with these lines:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
RewriteRule ^(.+?)/?$ /web/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/index.php [QSA,L]
</IfModule>

Categories