Htaccess wrongly making 301 redirect - php

I'm just trying to display css content at http://mydomain.com/dynamic_css/presets/
when user's browser loads http://mydomain.com/css/dynamic.css using the following
RewriteEngine on
RewriteRule ^dynamic\.css$ http://mydomain.com/dynamic_css/presets/ [QSA,L]
But instead browser gets http 301 redirected to http://mydomain.com/dynamic_css/presets/
Any idea why ?
Basically, the script at http://mydomain.com/dynamic_css/presets/ shows up a CSS generated code to allow more styles control from admin settings.

You can't specify the full domain in your request. mod_rewrite assumes this is an external URL which can only be handled via redirect. Try the below instead.
RewriteRule ^dynamic\.css$ /dynamic_css/presets/ [QSA,L]

Related

RewriteRule for blog posts with a subdomain in the URL

working on a writing a redirect rule but I'm not too familiar with them or with the syntax.
Basically I have Wordpress URLs that look like this:
https://example.com/blog/blog-title/garbagetext
I need to be able to redirect URLs like this to our 404 page, and my first attempt was with:
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
This worked, but it also made it impossible to access actual blog posts, and is also redirecting URLs that look like this:
example.com/blog/blog-title
Any ideas how to make this work properly?
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
:
example.com/blog/blog-title
The directive you posted could not redirect the example URL that you say is being erroneously redirected. So, either you have other directives (possibly in other .htaccess files, or the server config) or there is a conflict with existing directives, or you are seeing a cached response? If you've previously experimented with 301 redirects then these will have been cached aggressively by the browser. (It can be easier to test with 302 - temporary - redirects for this reason.)
Since you are using WordPress (which uses a front controller) then any redirects should appear at the start of the file, before the front controller.
You also appear to have an erroneous slash at the end of the RewriteRule pattern that will fail to match an intended URL of the form /blog/blog-title/garbagetext.
I'll ignore the backticks for now - I assume they are probably just a styling typo in your question?
I need to be able to redirect URLs like this to our 404 page
You shouldn't be redirecting to a 404 page. It should be served with an internal subrequest instead. If you redirect then you first send a 3xx response back to the client, the client then issues a second request for your 404.php page (exposing the URL) and then you have to make sure you are manually setting the appropriate 404 HTTP response header.
So, you need to define the appropriate ErrorDocument and R=404 the response.
The posts that need to be redirected to the 404 are any posts that have text following that trailing slash, so far example: example.com/blog/customer-reviews/asdfa.
Try the following instead, at the top of your .htaccess file:
ErrorDocument 404 /404.php
RewriteRule ^blog/.+/. - [R=404]
This will match /blog/customer-reviews/asdfa, but it won't match /blog/customer-reviews/ (or /blog/customer-reviews). The R=404 flag triggers an internal request to the defined error document. The L flag is not required when using a none 3xx code. When there is no substitution text (ie. no target URL) then specify a hyphen (-) instead.
And make sure you've cleared your browser cache.

Rewrite without changing URL in same domain using htaccess

I have a Two URLs
NEW URL :
http://host.ip.address/nl/business/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
OLD URL :
http://host.ip.address/nl/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
when in request NEW URL from browser it should retrieve content from OLD URL and the URL should not change in browser.
I have tried the below rule in .htaccess it is rewriting and redirecting to the OLD URL which should not do in my case
RewriteEngine on
RewriteRule (^|.*?/)nl/business/(.*)$ /$1nl/$2 [L,R=301]
It is not possible to do a redirect and keep the original URL in the address bar." This is because a redirect, by definition, involves telling the browser to ask for the requested resource at a different URL. So the browser updates its address bar and uses the new URL.
The simplest way would be use a rewrite, not a redirect. An internal rewrite simply serves content from a local file-path that is different from the requested local URL-path.
You can use something easy like this.
RewriteEngine On
RewriteRule nl/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart nl/business/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
You may need to also add
Options +FollowSymLinks

wordpress url rewriting issue for one page or post

I need help on wordpress Rewirite url.
http://www.example.com/this-is-a-test-title/93/
http://www.example.com/this-is-a-test-title2/93/
http://www.example.com/this-is-a-test-title3/93/
All these urls should point to
http://www.example.com/?p=93
any string after example.com/%%%%%%/93 should needs to point to
http://www.example.com/?p=93
Its should works only particular post id = 93 Only.
You may need to use couple of rewrite rules in web site .htaccess file, this file should be placed in root server directory. The mod_rewrite is an Apache module for manipulating (rewriting) URLs. Frequently this means taking the URL requested by a visitor and sending them content at a different URL.
Example:
Original URL:
http://www.example.com/old-page/
Desired destination URL:
http://www.example.com/new-page/
The .htaccess syntax:
RewriteEngine On
RewriteRule ^old-page/?$ $1/new-page$2 [R=301,L]
More info here.
Create an .htaccess and add the below rules in the file
Redirect 301 /this-is-a-test-title/93/ http://www.example.com/?p=93
Redirect 301 /this-is-a-test-title2/93/ http://www.example.com/?p=93
Redirect 301 /this-is-a-test-title3/93/ http://www.example.com/?p=93
there are plugins which help with these kind of redirects.
For e.g.
https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

htaccess url rewrite but executes the same page

i have an url like
http://example.com/folder1/folder2/admin_login.php
the above is active page. but i want to show the below url instead of above but functioning the same page.
http://example.com/folder1/folder2/login
So the question is display url-2 in browser but exeuctes url-1
i have tried this code in htaccess file
RewriteEngine on
RewriteRule ^folder1/folder2/admin_login.php$ http://example.com/folder1/folder2/login [NC,L]
When i put url-1 in browser address bar. it will convert into the url-2 but shows Page Not Found
You have it turned around. You want the second url to be in the address bar. That means you have to link to that url. When a request with that url comes to the server, the server has to internally translate that url to a working url. Besides that, you should not have the full url as a rewrite target, because this causes the rule to function as a redirect instead of an internal rewrite.
The following rule should do the trick:
RewriteRule ^folder1/folder2/login/?$ /folder1/folder2/admin_login.php [L]

How to stop direct execution of a php page using htaccess rules?

In my .htaccess file I have defined the following rule to make my register page URL as http://example.com/register/
RewriteRule register/ /register.php
The above rule is perfectly fine but I can access my register page from http://example.com/register/ as well as from http://example.com/register.php.
I don't want that user will be able to access the URL from http://example.com/register.php URL, is there any RULE which I can define in .htaccess to stop execution of register.php URL or simply redirect any direct register.php request to /register/
If you are doing this to avoid getting multiple links to the same content, you can simply don't use "register.php" anywhere on your page. I think no search engine will "guess" for a certain file type and if there are no security concerns you are on the safe side, because in my opinion no user will link to this file either. However if you want to be certain just reroute all your functionality through an index.php via one line in your .htaccess which should be placed inside your www-root directory:
RewriteEngine on
RewriteRule ^(.*?)$ index.php?file=$1
In your index.php you can then simply choose which function/file to invoke by breaking down and checking the $_GET["file"] parameter. To make 100% certain no one can access your register.php file directly just move it (and all your others) to a separate directory and include a .htaccess file with the following line:
DENY from all
There are a couple of other options to prevent direct access. Just define() a variable somewhere in your index.php and at the top of your register.php just put
defined('access') or die('Intruder alert!');
at the top. Another way could be to be honest and simply tell search engines that your content has been moved and that they no longer should use the old link:
header("Status: 301"); /* Content moved permanently */
header("Location: http://yourserver/Register/");
exit;
Update
Just one more thing that crossed my mind, you can also check $_SERVER["REQUEST_URI"], whether the user attached any ".php" and act accordingly by either denying access completely or just redirecting to the new location.
It is true that you cannot use location directive, but you can actually paste .htaccess file into any directory.
Just if you put this into it, say:
Options -Indexes
order allow,deny
deny from all
you can copy paste this file into any (root) directory you want to protect from external execution.
To check the initial requested URL path, you need to use the request line. So try this rule:
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php[/?\s]
RewriteRule (.+)\.php$ /$1 [L,R=301]
And then again your rule (in a slightly modified way):
RewriteRule ^register/$ register.php
If you want to completely block /register.php by using mod_rewrite, use a variant of SleepyCod's answer:
RewriteCond %{REQUEST_FILENAME} register\.php [NC]
RewriteCond %{IS_SUBREQ} false
RewriteRule .* - [F,L]
Explanation:
[NC]: Makes the condition case-insensitive, just in case you're on a windows box.
Condition 1: The requested filename is 'register.php', and
Condition 2: The request is no subrequest (this is important, since every new round through RewriteRules actually creates subrequests).
Rule: essentially do nothing
Flags: [F]: Send an 403 Forbidden header, [L]: This is the last rule to apply, skip all following rewrite rules
Rewriting correctly is an art by itself. I suggest you carefully read http://httpd.apache.org/docs/2.2/rewrite/.
Cheers,
Try this.
RewriteCond %{REQUEST_FILENAME} ^register\.php$ [NC]
RewriteRule ^/register register.php
Or this
Redirect register.php /register
Ignoring the user-experience part, you can implement the new rel=canonical link to sort out the search engines.
Although, for this case you should probably just use a 301 redirect from /register.php to /register/
In register.php
if ( stristr( $_SERVER['REQUEST_URI'], '.php' ) )
{
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: /register');
}

Categories