mod_rewrite - modify URL and continue with processing - mod-rewrite

This is excerpt from my .htaccess:
RewriteRule ^([0-9]+)x([0-9]+)$ images/$1x$2.png
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|css|rar|zip|tar\.gz)$ index.php [L]
What I am trying to achieve: When user goes to URL example.com/200x100, I want to internally rewrite the URL to example.com/images/200x100.png, so the application (front controller) sees the REQUEST_URI as /images/200x100.png, not just 200x100. If the file /images/200x100.png exists, the application should not be launched at all.

Related

How to convert .htaccess to web.config?

I am realy lost here. I am used to work with linux server so I use .htaccess
Now I need to develop a project in windows server and I need to use some rewrite rules, etc. So I need to set the web.config file.
So I need help to convert my .htaccess rules to web.config
This are the rules:
RewriteEngine On
RewriteRule ^box/camp(.+)?/?$ /_box/access_box_files/camp$1
RewriteRule ^box/merchant(.+)?/?$ /box/access_box_files/merchant$1
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_tab=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
UPDATE
RewriteRule ^box/camp(.+)?/?$ /_box/access_box_files/camp$1
In this case I need that whenever the URL is "/box/camp" there is access to "/_box/access_box_files/cam" and that all the information that comes in front of "(.+)?" "$1" is included. Without changing the URL in the browser, or forwarding the user.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url_tab=$1 [L,QSA]
In this case, any information that comes in the URL must be collected in a variable.
For example:
https:// website dot com/tab1/tab2/tab3
I will get the information in: $_GET['url_tab'] which would have the information: "tab1/tab2/tab3".
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In this case it is to force the URL to be loaded with https.
Thank you all very much.

Why Does index.php reappear in url even after removing it via htaccess

My question is that, I have removed index.php in codeignitor 3 via following code in .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
but when i redirect from one controller to another index.php reappear in the URL, As far as i am working within same controller it remains hidden.

Laravel serving up pages from invalid route

I have the following route: Route::resource('users', 'ProfileController'); When I go to site.com/users/123, it loads the page properly. I can also go to site.com/index.php/users/123 and get the same page (not sure if laravel is by default intended to do that).
If I change the url to site.com/does_not_exist/users/123, laravel returns a 404, as you would expect. However, if I go to site.com/does_not_exist/index.php/users/123, laravel loads the page. I can put any random path, non-existent path between site.com and index.php and it will work.
Why doesn't laravel return a 404 for this? How do I fix it so it does?
Because the index.php is the index.php in public folder that handles the incoming request and its a automatically generated class loader
This might work (similar question on how to fix this)
1-Renaming the server.php to index.php (no modifications)
2-Copy the .htaccess from public (like rimon.ekjon said below)
3-Changing .htaccess it a bit as follows for statics:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
If there are any other static files needed just add the extension to the previous declared list
Source

Why isn't this mod_rewrite re-directing?

I'm sending every request through an index.php except for pages in my blog subdirectory. I've been able to do this using mod_rewrite in my parent folder and;
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
]
However, I'd also like to send requests to my blog folder if they have the form:
documentation/some-file.
I've tried:
RewriteRule ^/documentation/(.+)$ https://www.some_domain.com/blog/documentation/$1
but it looks like my request isn't getting sent to the blog folder in this case. My full code is below:
RewriteEngine On
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/documentation/(.+)$ https://www.some_domain.com/blog/documentation/$1
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Edit:
I've used a slightly modified version of #Rijul's suggestion below and after moving the RewriteRule to before the RewriteCond, it works as I had hoped. In other words, the re-write for documentation performs the re-write to the blog subfolder. And, all other requests go through my index.php file. At this point, I would like to understand why.
RewriteEngine On
RewriteRule ^documentation/?(.*)$ /blog/documentation/$1 [R=301,L]
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
From what i know
RewriteEngine On
Without this all the rewrite rules and condition will be ignore
RewriteRule ^documentation/?(.*)$ /blog/documentation/$1 [R=301,L]
Rewrites documentation to blog/documentation
RewriteCond %{REQUEST_FILENAME} !-d
This rewrite condition checks whether the requested directory name doesn't exits. If it doesn't exits. Then proceeds to the next condition
RewriteCond %{REQUEST_FILENAME} !-f
This rewrite condition checks whether the requested file name doesn't exits. If it doesn't exits. Then proceeds to the next condition
RewriteCond %{REQUEST_FILENAME} !-l
This rewrite condition checks whether the requested symbolic link doesn't exits. If it doesn't exits. Then proceeds to rewrite rule
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Rewrite to index.php?url=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
writing rewrite condition like this produces an and operation.
(no file exits in that name) and (no directory exits in that name) and
(no symbolic link exits in that name)
If this is true then rewrite to php file. (no directory exits in that name) will go false in the case for /blog (since such a directory exits)

mod_rewrite disallow call of subfolder

I am using 2 Domains like http://doma.in and http://domain.com
The shortning URL calls only generated URL's from my Script. All other calls from the shortning URL should be forwarded to the Main Domain. Thats why I have this small mod_rewrite Rule.
RewriteCond %{HTTP_HOST} ^doma.in [NC]
RewriteRule ^/?$ http://www.domain.com/$1 [R=301,L]
The Question
I want to disallow calling subfolders from the shortning URL. Because the shortning URL can also generate Custom URLs like
http://www.nokia.com -> http://doma.in/nokia
If I would have a subfolder called "nokia" than it will not be forwarded to the target. Instead of to forward it is calling the subfolder.
To forward URLs over mod_rewrite is this Rule used.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-x
RewriteRule ^(.*) /redirect.php?id=$1 [L]
Your question is a little unclear.
If you want the redirect to execute for /nokia even if you have a directory called /nokia, you need to remove the line:
RewriteCond %{REQUEST_FILENAME} !-d
What that condition says is "If the file path of the request is an existing directory, do not continue."

Resources