Is it safe to remove "RewriteBase /" within this context [duplicate] - .htaccess

This question already has answers here:
How does RewriteBase work in .htaccess
(9 answers)
Closed 8 years ago.
I am working on a codeigniter site. In the .htaccess file I found this rule:
RewriteBase /
After reading up on RewriteBase, I started to wonder if this rule actually does anything within this context (if the htaccess file is placed within the root folder.) Is it safe to remove?
Even after reading the documentation and other posts, I am still very confused about what this does. Is this RewriteBase directive needed if the .htaccess file is in the root folder? WIll it behave differently within htaccess files located in folders that are not?

The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.
So whenever you use something that requires a relative path in the RewriteRule statements, it will use slash.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

Related

Allow specific URL in htaccess even if htaccess rewrites a word from that URL

I have the following situation in my .htaccess:
RewriteEngine On
RewriteRule ^laravel/(.*)$ /$1 [R=301,L]
This is specifically made to not allow people to visit my laravel directory.
However, I want to be able to load a specific file from laravel directory into other files, like this:
<script src="/laravel/public/js/app.js" defer></script>
The problem is the following:
The generated URL will have 'laravel' removed from it as per the rule. If I comment that rule, then that line of code that includes app.js will work.
I have tried several things with my .htaccess and searched for a solution, but alas, I am failing to understand, it seems, how .htaccess code really does the things.
Can anyone help with a rule to allow specifically that URL?
Or, if possible, to allow access to the /laravel/public/js/ directory without removing the word 'laravel' from the URL.
Thank you very much!
Instead of doing complex things with checking negated patterns in a RewriteCond or similar, you could just put a rule before this that matches that URL specifically, does no rewriting at all (- in place of substitution URL), and then uses the L flag to indicate that none of the following rules should be evaluated any more.
RewriteRule ^laravel/public/js/app\.js$ - [L]

Eliminate duplicate directory name in url using .htaccess file

For some reason, I'm getting duplicate directory names in some urls within a subfolder on our website. This seems to affect only crawlers as the files within this directory work fine when navigated.
I'd like to simply remove the duplicate directory name and make mydomain.com/sub/sub redirect to mydomain.com/sub.
I've tried many versions but my .htaccess skills are lacking apparently. I currently have (not working of course):
RewriteRule ^mydomain.com/sub/sub/(.*) mydomain.com/sub/$1 [L,R=301]
RewriteRule ^mydomain.com/sub/sub/(.*) mydomain.com/sub/$1 [L,R=301]
The RewriteRule pattern matches against the URL-path only - you appear to have included (part of) the domain name. Also, mydomain.com in the substitution string is going to be seen as a relative subdirectory.
Assuming you have a limited number of subdirectories where this occurs then to reduce /sub/sub/<something> to just /sub/<something> you would do something like this:
RewriteEngine On
RewriteRule ^sub/sub/(.*) /sub/$1 [R=301,L]
If you have other directives in you .htaccess file, then this needs to go near the top.
First test with 302 (temporary) redirects to avoid potential caching issues. Clear your browser cache before testing.
But to echo #arkascha's comment... the reason why crawlers are finding these URLs in the first place would seem to be a fault in your URL structure/internal links - so this is what ultimately needs to be fixed.

htaccess url-rewrite specific file to subfolder

I've been searching for hours now hoping to find a solution for my problem. But all I find is something like an URL-rewrite from folder to folder. Nothing related to my question.
Well I'm trying to achieve an URL-rewrite from a script (e.g. confirm.php) to a script in a subfolder (e.g. scripts/confirm.php).
Something like this
www.example.com/confirm.php?id=somevalue&key=somevalue
to
www.example.com/scripts/confirm.php?id=somevalue&key=somevalue
where the values of the parameters are variable and the page can be visited via http/https and with or without www.
Try below rule, in your root directory, I am assuming confirm.php is not exist in root directory.
RewriteEngine On
RewriteRule ^confirm\.php$ scripts/confirm.php [QSA,L]

htaccess for local testing and live server

I'm close, to my final solution I think.
The .htaccess looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /myproject/development/
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]
RewriteRule ^((?!index\.php)[^/]+)/([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/?$ index.php?page=$1&keyword=$2&zip=$3&range=$4 [L,R]
I don't need the RewriteBase for the 1st rule(was a little surprised about that) but I need it if I add the 2nd rule and open this URL:
//localhost/myproject/development/somepage/test/13245/50
Otherwise the page will be opened but of course without the stylesheets and javaScripts can be found then.
1.) Target: I want to use the 2nd rewriteRule without changing or adding a rewriteBase. What do I need to change in the .htaccess so I can keep testing my project without a rewriteBase.
Why: As I asked before I want to test my project locally and on the live-server without changing too much on the project configuration.
2.) The [R] Flag If I request
//localhost/myproject/development/somepage/test/53229/2000
Of course in the adressline then we have this URL
//localhost/myproject/development/index.php?page=somepage&keyword=test&zip=12345&range=2000
To avoid this behaviour I simply should remove the R-Flag. But then the CSS and JS can't be found anymore. Also Here I'm looking for a solution without rewritebase, basepath, virtual host, etc. if possible.
Here is where I started:
Rewrite rules for localhost AND live envoirement
By the looks, you don't want the R flag on the 2nd RewriteRule. That defeats the object of your "pretty" URLs.
But then the CSS and JS can't be found anymore.
Because you are using relative paths to your CSS and JS files. Either change your paths to root-relative (starting with a slash), or use the base element in the head section of your pages, to indicate the URL that all relative URLs are relative to:
<base href="http://www.example.com/page.html">
More Information:
https://developer.mozilla.org/en/docs/Web/HTML/Element/base
I don't need the RewriteBase for the 1st rule(was a little surprised about that)
You don't need the RewriteBase for the 1st rule (an internal rewrite) because the directory-prefix (the filesystem path that lead to this .htaccess file) is automatically added back on relative path substitutions.
However, for external redirects (ie. R flag), the directory-prefix does not make sense, so you either need to specify a root-relative (starting with a slash) or absolute URL. Or specify the appropriate RewriteBase directive, which overrides what URL-path will be added for relative substitutions (that's all it does).

Best of both world - MVC site and access to directory structure [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a combined website and web application on a domain, and I'd like to rewrite the website portion to use a more MVC framework like Symfony2.
I'd like to use the www.example.com/index.php/pagename approach which I could then replace with modrewrite to work as www.example.com/pagename.
Problem -- I am not prepared to refactor the entire web application. For example, there are sever folders with literal files that need to be accessed.
www.example.com/admin/dosomething.php
So I wondering about best ideas for rewriting index.php using something a little more attractive while preserving the file directory structure in the URLs.
Thoughts/options?
EDIT: To clarify, I want to rewrite example.com/index.php/page to something more professional while still allowing access to http://www.example.com/admin/file.php, an actual path. Looking for thoughts and best practices for that.
The default .htaccess file from Symfony Standard Edition handles this by checking for file existence before rewriting. The logic is roughly "if this request matches an existing file, don't rewrite... else, rewrite to app.php" (or in your case index.php).
It accomplishes this by using rewrite conditions combined with a rewrite rule with a - (dash) as the substitution (thus indicating that no substitution should be performed):
# If the requested filename exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? app.php [L]
If you want to let existing directories be served directly too (instead of being redirected to index.php), use the -d switch, for example by adding this at the top:
# If the requested directory exists, simply serve it (except the root, which
# should also be rewritten - the ".+" pattern requires the path to be at least
# one character long, thus ignoring the root).
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

Resources