For specific url 'www.example.com/mobile' I want url rewrite to another specific path: 'root_folder/mobile/index.html' - apache

I created a website in Symfony and the mobile version in Ionic.
Now all URLs are redirected to /public/index.php.
I want to add another URL rewrite in .htaccess to redirect all www.example.com/mobile/* to ionic app that is in the root folder /mobile/index.html.
I already tried to add this in root_folder/mobile/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
My root_folder .htaccess is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.ch$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|svg|webp|mp4|css|js|txt)$
RewriteRule ^(.*)?$ public/index.php [L]
<If "%{REQUEST_URI} =~ m#/mobile/?#i">
RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
</If>

RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
The RewriteRule pattern matches against the URL-path relative to the directory that contains the .htaccess file. So, the above RewriteRule will never match when inside the /mobile/.htaccess file. Likewise, the condition will never be successful, since if the .htaccess is triggered then /mobile/ must already be present in the URL-path.
This is also an external 301 redirect, not an internal rewrite. It needs to be an internal rewrite (just like the rewrite to public/index.php in the root).
However, presumably you also want static assets to be ignored and served directly, so you need exceptions for files (and optionally directories), just as you are doing in the root .htaccess file (when rewriting to public/index.php).
Try the following instead, in the /mobile/.htaccess file:
# /mobile/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
You don't need to specify /mobile in any of the directives since the RewriteRule pattern matches against the relative URL-path (as mentioned above) and the substitution string is (by default) relative to the directory that contains the .htaccess file.
By default, the mod_rewrite directives in the /mobile/.htaccess file completely override the directives in the parent .htaccess file. The directives in the parent .htaccess file are not even processed.
<If "%{REQUEST_URI} =~ m#/mobile/?#i">
RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
</If>
You should remove this <If> block in the root .htaccess file. (It's not actually doing anything.)

Related

Apache ModRewrite rules for frontend controller AND `301 Permanently Moved`

What I'm doing right now
I'm developing a backend for a customer.
Previously he mixed up the website data with files to download for friends and other purposes.
Example:
/public
/somedir
somesubdirfile
anotherfile1
anotherfile2
foobar.html
index.html
Now I implemented the common rewrite rules to proxy all requests to the new index.php of the website.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine "On"
RewriteCond "%{REQUEST_FILENAME}" ".htaccess"
RewriteCond "%{REQUEST_FILENAME}" ".htpasswd"
RewriteCond "%{REQUEST_FILENAME}" "!-s"
RewriteCond "%{REQUEST_FILENAME}" "!-l"
RewriteCond "%{REQUEST_FILENAME}" "!-d"
RewriteRule "^.*$" "public/index.php" [NC,L]
</IfModule>
Fine, works.
Now I moved all the clutter into a separate folder.
/resources
/somedir
somesubdirfile
anotherfile1
anotherfile2
foobar.html
/public
index.php
What I need to accompolish
... is to change the rewrite rules.
If the request doesn't match a specific file or symlink in /public it must be tested if it matches a specific file or symlink in /resources. If there's a match a 301 Moved Permanently must be sent and the request redirected to /resources. If there's no match a redirect to the public/index.php must be done.
But I really stuck in understanding how to write proper rewrite rules in a complexity like I'm requesting.
I need some help here, please.
You may try these rules in your site root .htaccess:
RewriteEngine On
# if it exists in /resources then redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/resources/$0 -f
RewriteRule .+ /resources/$0 [L,NE,R=301]
# else route to public/index.php
RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd) [NC]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]

How to .htaccess pseudo-static + hidden extension?

Why can't I use the following?
RewriteRule ^(.*)$ $1.html [L]
.htaccess file:
RewriteRule ^(.*)$ index.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^(.*)$ index.php [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
The first rule rewrites everything to index.php. The directives that follow are effectively ignored.
However, the second rule also rewrites everything (same pattern ^(.*)$ - obvious conflict), that doesn't map to an existing file or directory to append a .html extension. This second rule is more restrictive.
It seems that what you want to do is:
Append the .html extension to URLs that would map to .html files.
Rewrite all other requests that (I assume) do not map to physical files and directories to index.php.
Additional assumptions
The .htaccess file is located in the document root.
Requested URLs that should map to .html files do not contain dots in the URL-path. Therefore, dots in the URL-path indicate file extensions only.
If you literally rewrite everything else to index.php (as you were doing) then it will also rewrite all your static resources (CSS, JS, images, etc.), so I assume you want to make exceptions for static resources and anything that would otherwise map to a file or directory.
Try the following instead:
RewriteEngine On
# Append the ".html" extension if the target file exists
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^.])$ $1.html [L]
# Rewrite other requests that don't map to files/directories to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(?:css|js|jpg|png|gif)$ index.php [L]

Removing .html extensions in .htaccess file

I am trying to configure my site with an htaccess file so that any request ending in .html will be rewritten so that the new URL is identical except that ".html" is not present at the end. This should work regardless of how many directories deep the request might be.
So for example,
http://site.com/page1.html -> http://site.com/page1
http://site.com/dir1/dir2/page2.html -> http://site.com/dir1/dir2/page2
http://site.com/~bob/dir3/page3.html -> http://site.com/~bob/dir3/page3
I would prefer a solution where I don't need to hardcode the domain name, but it would be acceptable if necessary.
My current .htaccess file is a standard Zend Framework .htaccess file, and it is working correctly. I am aware that I will need to change the rewrite base if I move the site elsewhere.
SetEnv APPLICATION_ENV development
RewriteEngine On
#Send all requests for non-existant files to index.php
RewriteBase /~rburk/refactor
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /(.*)\.html($|\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]

htaccess remove index.php from url

I have a problem whereby google has indexed some pages with the wrong url.
The url they are indexing is:
http://www.example.com/index.php/section1/section2
I need it to redirect to:
http://www.example.com/section1/section2
.htaccess isn't my forte, so any help would be much appreciated.
The original answer is actually correct, but lacks explanation. I would like to add some explanations and modifications.
I suggest reading this short introduction https://httpd.apache.org/docs/2.4/rewrite/intro.html (15mins) and reference these 2 pages while reading.
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
https://httpd.apache.org/docs/2.4/rewrite/flags.html
This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.
mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1 [L]
Think %{REQUEST_FILENAME} as the the path after host.
E.g. https://www.example.com/index.html, %{REQUEST_FILENAME} is /index.html
So the last 3 lines means, if it's not a regular file !-f and not a directory !-d, then do the RewriteRule.
As for RewriteRule formats:
So RewriteRule (.*) /index.php/$1 [L] means, if the 2 RewriteCond are satisfied, it (.*) would match everything after the hostname. . matches any single character , .* matches any characters and (.*) makes this a variables can be references with $1, then replace with /index.php/$1. The final effect is to add a preceding index.php to the whole URL path.
E.g. for https://www.example.com/hello, it would produce, https://www.example.com/index.php/hello internally.
Another key problem is that this indeed solve the question. Internally, (I guess) it always need https://www.example.com/index.php/hello, but with rewriting, you could visit the site without index.php, apache adds that for you internally.
Btw, making an extra .htaccess file is not very recommended by the Apache doc.
Rewriting is typically configured in the main server configuration
setting (outside any <Directory> section) or inside <VirtualHost>
containers. This is the easiest way to do rewriting and is recommended
To remove index.php from the URL, and to redirect the visitor to the non-index.php version of the page:
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will cleanly redirect /index.php/myblog to simply /myblog.
Using a 301 redirect will preserve Google search engine rankings.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
Assuming the existent url is
http://example.com/index.php/foo/bar
and we want to convert it into
http://example.com/foo/bar
You can use the following rule :
RewriteEngine on
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [NE,L,R]
#2)internally map "/foo/bar" to "/index.php/foo/bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]
In the spep #1 we first match against the request string and capture everything after the /index.php/ and the captured value is saved in %1 var. We then send the browser to a new url.
The #2 processes the request internally. When the browser arrives at /foo/bar , #2rule rewrites the new url to the orignal location.
Steps to remove index.php from url for your wordpress website.
Check you should have mod_rewrite enabled at your server.
To check whether it's enabled or not - Create 1 file phpinfo.php at your root folder with below command.
<?php
phpinfo?();
?>
Now run this file - www.yoursite.com/phpinfo.php and it will show mod_rewrite at Load modules section.
If not enabled then perform below commands at your terminal.
sudo a2enmod rewrite
sudo service apache2 restart
Make sure your .htaccess is existing in your WordPress root folder, if not create one .htaccess file
Paste this code at your .htaccess file :-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Further make permission of .htaccess to 666 so that it become writable and now you can do changes in your wordpress permalinks.
Now go to Settings -> permalinks -> and change to your needed url format.
Remove this code /index.php/%year%/%monthnum%/%day%/%postname%/
and insert this code on Custom Structure: /%postname%/
If still not succeeded then check your hosting, mine was digitalocean server, so I cleared it myself
Edited the file /etc/apache2/sites-enabled/000-default.conf
Added this line after DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
Restart your apache server
Note: /var/www/html will be your document root
Do the following steps
1. Make sure that the hosting / your pc mod_rewrite module is active. if not active then try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.
change this setting :
#LoadModule rewrite_module modules/mod_rewrite.so
to be and restart wamp
LoadModule rewrite_module modules/mod_rewrite.so
2. Then go to .htaccess file, and try to modify to be:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
if above does not work try with this:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
3. Move .htaccess file to root directory, where is index.php there.
www OR root folder
- index.php
- .htaccess
Some may get a 403 with the method listed above using mod_rewrite. Another solution to rewite index.php out is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
# Put your installation directory here:
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
I have used many codes from the above mentioned sections for removing index.php form the base url. But it was not working from my end. So, you can use this code which I have used and its working properly.
If you really need to remove index.php from the base URL then just put this code in your htaccess.
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
This will work, use the following code in .htaccess file
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
I don't have to many bulky code to give out just a little snippet solved the issue for me.
i have https://example.com/entitlements/index.php rather i want anyone that types it to get error on request event if you type https://example.com/entitlements/index
you will still get error since there's this word "index" is contained there will always be an error thrown back though the content of index.php will still be displayed properly
cletus post on "https://stackoverflow.com/a/1055655/12192635" which
solved it
Edit your .htaccess file with the below
to redirect people visiting https://example.com/entitlements/index.php to 404 page
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
to redirect people visiting https://example.com/entitlements/index to 404 page
RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
Not withstanding we have already known that the above code works with already existing codes on stack see where i applied the code above just below the all codes at it end.
# The following will allow you to use URLs such as the following:
#
# example.com/anything
# example.com/anything/
#
# Which will actually serve files such as the following:
#
# example.com/anything.html
# example.com/anything.php
#
# But *only if they exist*, otherwise it will report the usual 404 error.
Options +FollowSymLinks
RewriteEngine On
# Remove trailing slashes.
# e.g. example.com/foo/ will redirect to example.com/foo
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP if it exists.
# e.g. example.com/foo will display the contents of example.com/foo.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
RewriteCond %{THE_REQUEST} \index[\ /?].*HTTP/
RewriteRule ^.*$ - [R=404,L]
try this, it work for me
<IfModule mod_rewrite.c>
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
For more detail
create .htaccess file on project root directory and put below code for remove index.php
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Ignore symlinks in clean URL's in .htaccess

Example URL:
example.com/user
/user is both a symlinked directory and a valid URL to content on my site. I user Horde Routes to request the content and all requests to the site go through index.php.
I currently have a .htaccess file that looks like:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#allow cool urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
#allow to have Url without index.php
But going to /user lists the directory contents rather than the webpage. Is it possible to ignore symlinks?
Additional to that is if you request:
example.com/user/some-css-file.css
That is a valid request that should not be ignored. So is it possible to allow files via symlinks to be requested, but the base symlinks themselves to be ignored and go to index.php?
Thanks :)
The test for !-d will fail when /user/ is requested since it’s actually an existing directory. You might want to use it without that condition and only allow direct access to existing files but not directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [L]
Additionally you could replace the pattern ^(.*) with !^index\.php$ so that a request for the index.php doesn’t require a filesystem lookup:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ index.php [L]
Forget ignoring symlinks just create another RewriteRule. Place it before the "allow cool urls" rule:
RewriteCond %{REQUEST_URI} ^/user/$
RewriteRule (.*) /index.php [L]
So http://www.example.com/user/ or http://www.example.com/user should go to the content. The [L] should prevent further rules from being processed.