I'm trying to find a way to do the following:
example.com
When requesting:
https://example.com/folder1/folder2
https://example.com/folder8374/folder8749
....
Do this:
Prevent 404 not found error
Open index.php, but keep uri the same and exclude index.php (https://example.com/../...)
Is there any way to solve this?
Thanks and kind regards
You are looking for the concept of a general router for your application logic. This certainly is possible by a simple single internal rewrite:
RewriteEngine on
RewriteRule ^ /index.php [QSA,END]
A variant would be that:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [QSA,L]
That's all. You can implement such a rule in the http server's host configuration. Or, if you really have no access to that you can also use a distributed configuration file (".htaccess"), you need to enable the interpretation of such files though. Typically such file is placed in the top level of your application logic.
Related
I need help with .htaccess redirect.
There is a lot of simmilar answers, but did not find the one for my case.
I ve got this config:
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Now I need redirection for all url but not for 'news_list'
This work for all redirections:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ https://www.newsite.com/$1 [R=301,L]
</IfModule>
For exception I should use:
RewriteRule !^news_list($|/) https://www.newsite.com/$1 [R=301,L]
This works - 'news_list' do not redirect, but problem is, that than '/news_list' doesn't work (because it need point to 'index.php?url=news_list'.
So I need kinda mix of all configs above, but no luck.
Thank you in advance!
This probably is what you are looking for:
RewriteEngine On
# Rewrite everything to new host except for /news_list...
RewriteCond %{REQUEST_URI} !^/news_list(?:$|/)
RewriteRule ^ https://www.newsite.com/$1 [R=301,END]
# Rewrite to index.php if not file or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ /index.php?url=$1 [QSA,END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
my htaccess file looks like this:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.php$ sitemap.xml [L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
I want my links look like:
https://kaznews.kz/news/477800
as they are at the time,
but when I have QUERY STRING they not opening links like: https://kaznews.kz/news/477800?google
I want either delete the ?mark and the query, or add them at the end but show the correct page.
RewriteRule ^(.*)$ /index.php?/$1 [r=301,L,QSA]
this is not suitable for me because it gives me
such result: https://kaznews.kz/index.php?/news/477800&google with index.php inside, but there will be duplicate links then.
Well, looks like that is roughly what you are looking for:
RewriteRule ^/?news/(\d+)/(.+)$ /news/$1?$2 [END,QSD]
That rule will internally rewrite requests to /news/477800/google to /news/477800?google.
Update:
From your comments below we learned that what you actually appear to ask is how you can remove, so ignore any query arguments specified in the request. Though this is a questionable thing to do (as reasoned in the comments) here is a rule to achieve that:
RewriteRule ^/?news/(\d+)$ /news/$1 [END,QSD]
General:
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I have a .htaccess file in my server to rediect my subdomain website index folder to public.
However, after the redirection, css, js, img file all cannot be load.
How to fix it?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.subdomain.domain\.com.sg$
RewriteRule ^/?$ "http\:\/\/subdomain.domain\.com.sg\/" [R=301,L]
DirectoryIndex public/index.php
Edited :
I have update my htaccess file to this.
the index file is loaded correctly, but can i remove the "public" from the url?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.subdomain.domain\.com.sg$
RewriteRule ^/?$ "http\:\/\/subdomain.domain\.com.sg\/" [R=301,L]
RewriteRule ^index.php$ subdomain.domain.com.sg/public/index.php [R=301,L]
sorry I am new to apache environment
With "can i remove the "public" from the url?" you probably ask if you can internally rewrite incoming requests to the /public folder. Certainly you can. Question is if you want only that index.php file to be redirected or all requests beneath that. Note that these rules implement an internal rewriting, so do not alter the URL visible in the browser which is most likely what you want:
# http://subdomain.example.com/index.php => /public/index.php
RewriteRule ^/?index.php$ /public/index.php [L,QSA]
A more general approach:
# http://subdomain.example.com/foo/bar => /public/foo/bar
RewriteRule ^/?(.*)$ /public/$1 [L,QSA]
If you register index.php in your http servers host configuration as index document, then you can even drop that from the URL. Documentation: https://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex:
# http://subdomain.example.com/ => /public/index.php
DirectoryIndex index.php
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I try all posible, i need redirect this url's type:
http://www.midominio.com/esp/productos_listado.php?id=98
with id [1-4504] To
http://www.midominio.com/categorias/98-libro.html
I prove:
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)
RewriteRule ^id=(.*)$ /categoria/%1$1-libro.html? [R=301]
But only rewrite:
http://www.midominio.com/categorias/-libro.html
Without var nu,be
It is unclear from your description in what direction you actually want the rewriting to be applied.
If the incoming request to be rewritten is /categorias/..., then something like this should be just fine:
RewriteEngine on
RewriteRule ^/?categorias/(\d+)-libro.html /esp/productos_listado.php?id=$1 [L]
If however, as your phrasing suggests, you have the unusual situation that you want to rewrite incoming requests to /esp/productos_listado.php, then try it this way 'round:
RewriteEngine on
RewriteCond %{QUERY_STRING} id=(\d+)
RewriteRule ^/?esp/productos_listado\.php /categorias/%1-libro.html [L]
That notation would be working for .htaccess style files and also in the real host configuration. Note that if you have access to the host configuration, then you always should prefer placing the rules in there. .htaccess style files are notoriously error prone, hard to debug and they really slow the http server down. They are only offered for situations where people do not have access to the host configuration. So for example when using really cheap hosting service providers...
I need to edit my .htaccess to do something like this:
from URL example.com/tag/iphone/iphone-manual to URL:
example.com/iphone/iphone-manual
I just want to remove the tag from its permalink. I don't know whether this could be achieve only by changing htaccess or it had to edit using PHP too.
Here is my current htaccess:
RewriteEngine On
RewriteRule ^tag/.* /tag.php [QSA]
RewriteRule ^([^/]+)/$ a-search.php?q=$1
I assume you want to replace the previous "tag rule" inside your .htaccess file? Because it conflicts with what you ask in this question. I'd say all you need to do is this:
RewriteEngine On
RewriteRule ^tag/(.*)$ $1 [QSA,L]
RewriteRule ^([^/]+)/$ a-search.php?q=$1 [L]
If instead you want to add a specific rule, so an exception, then this probably is what you are looking for:
RewriteEngine On
RewriteRule ^tag/iphone/(.*)$ iphone/$1 [QSA,L]
RewriteRule ^tag/.* /tag.php [QSA,L]
RewriteRule ^([^/]+)/$ a-search.php?q=$1 [L]
A general remark: .htaccess style files are notoriously error prone, they make things complex, are hard to debug and really do slow down the server. They they should only be used in two situations:
if you do not have access to the real host configuration (otherwise palce the rules in there!)
if you require dynamic changes to the rule set by some web application (though think twice about the security implications)
In all other cases it makes much more sense to use the real host configuration instead of .htaccess style files.