.htacces redirect all traffic to index.php and request for .js to another file - apache

Hi Im Not very comfortable writing htaccess rules, my goal is
Redirect all traffic to index.php
Redirect all request to script.min.js to script.php
ultimately scripts.min.js does not exist rather the script.php will have set headers and include all needed JS files to create one master file. Also the status code should be 200.
Thank you in advance

I haven't tested this, but it'd probably look something like:
RewriteEngine On
RewriteRule ^script.min.js$ script.php [L]
RewriteCond %{REQUEST_URI} !/index.php$ [NC]
RewriteRule (.*) /index.php [L]

I was able to solve the problem by taking wordpress .htaccess and prepending the file match section
<IfModule mod_rewrite.c>
RewriteEngine On
<FilesMatch "\.(js)$">
RewriteRule . /scripts.php [L]
</FilesMatch>
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Related

How to redirect ourdomain.ch/index.php?xyz to ourdomain.ch?

Due to a past hacking of the site we have hundreds of indexed files in Google (site:ourdomain.ch) like this:
ourdomain.ch/index.php?02374234999
ourdomain.ch/index.php?wer99234234
1) These URL's are not redirected and show the content of the homepage, just with another URL (see above). I guess this is a problem SEO wise, i.e. duplicate content. Right?
2) I am not able to redirect these files to the root. No way what i do. I added that line to the existing .htaccess:
RewriteRule ^index.php\?123 /index.php [R=301,L]
just to test it with ourdomain.ch/index.php?123
it redirects to: ourdomain.ch/?123. Not what i want...
Updated .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php\?123 /index.php [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any hint how to accomplish that?
Thanks a lot for your help.
Cheers,
Cesare
You need to use RewriteCond
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^index\.php$ /index.php? [L,R=301]
This will redirect /index.php?foobar to /index.php removing the querystring from your index.php .

Rewrite rule for opening a different page

I'm stuck with some .htaccess rules.
I've got the page /module/slug. I want, when I type slug to see content of /module/slug/ but not redirect to this page. Is it possible? I've tried this rule, but had no luck:
RewriteRule ^slug/$ /module/slug/ [L]
My .htaccess content:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^calculator/$ /sample-page/ [R=301,L]
</IfModule>
# END WordPress
That happens because you don't tell Apache to redirect (R=301).
Add the redirect flag R, and it should work. Also make sure that:
You have set the RewriteBase correctly.
There are not any other rules earlier in the file that match the same URL.
.
RewriteEngine On
RewriteBase /
RewriteRule ^slug/$ /module/slug/ [R=301,L]

Remove specific slug in with htaccess

I have a Wordpress site web in localhost with XAMPP. Now I have a URL like this:
http://localhost:82/subdirectory/images.html/nggallery/tags/[tagname]
Now I want this:
http:// localhost:82/subdirectory/images/nggallery/[tagname]
To remove tags, I've tried many codes, like this:
RewriteRule ^tags/(.+)/$ /$1 [R=301,L]
but doesn't work.
Help me, please.
I'm using WordPress, my htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>
# END WordPress
Before answering, a better idea than using htaccess for this is to use Wordpress' rewrite modules.
Anyway, you need to put your specific rules before Wordpress' main rule.
You can replace your current code by this one in your htaccess (which has to be in your subdirectory folder)
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /subdirectory/
# redirect /.../tags/XXX to /.../XXX
RewriteCond %{THE_REQUEST} /images\.html/nggallery/tags/([^\s\?]+)\s [NC]
RewriteRule ^ images/nggallery/%1? [R=301,L]
# internally rewrite /.../XXX to /.../tags/XXX
RewriteRule ^images/nggallery/(.+)$ images.html/nggallery/tags/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
# END WordPress
try
http:// localhost:82/subdirectory/images/nggallery/[tagname]
RewriteRule ^nggallery/tags/(.*)$ http://localhost:82/subdirectory/images/nggallery/$1

.htaccess web/ + secure directory 1and1 Webserver

I am running symfony2.3. My .htaccess looks like this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule ^/web/ - [L]
</IfModule>
I always have to enter www.example.com/web/en/. How can I get rid fo web/ ?
I also can get to files to www.example.com/composer.json. This should not happen. How can I secure it?
TIA!
Changing this line
RewriteRule ^/web/ - [L]
to
RewriteRule ^/ - [L]
should help with the first issue.
The second issue isn't possible if that file is used on a webpage. If a browser can access it then a user can.
This works.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/$1 [QSA,L]
</IfModule>
But now I have www.example.com/de/ also www.example.com/web/de/. Is there a way to have only the first was?

Wordpress .htaccess errors

Thanks in advance to all you .htaccess guru's out there. I'm having an issue that's been bothering me for some time. I have wordpress nested inside my own site folder structure. To the user, everything is cool. But when i go to www.domain.com/blog/wp-admin, my credentials will not log in. It just keeps looping me around to the same page. If I go to www.domain.com/blog/wp-login.php directly it works. I wonder if anyone knows why this is.
The blog is located at /fe/blog/.
The .htaccess in / is:
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteRule ^(files)/?(.*) /files/$2 [NC,L]
RewriteRule ^(.*) /fe/$1 [NC]
The .htaccess in /fe is:
Options +FollowSymlinks -Indexes
RewriteEngine on
And Finally the .htaccess in the /fe/blog/ is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
Any help would be greatly appreciated!
Try this in your /.htaccess file:
RewriteEngine on
RewriteRule !^(files|fe)/ fe%{REQUEST_URI} [NC]
And this in your /fe/blog/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
i wonder if this line is not the culprit:
RewriteRule ^(files)/?(.*) /files/$2 [NC,L]
could you try:
RewriteRule ^files/?(.*) /files/$1 [NC,L]
If not, you might also add the flag [QSA] (Query String Append)
RewriteRule ^files/?(.*) /files/$1 [NC,L, QSA]