htaccess links with query strings not opening on pc, - .htaccess

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).

Related

Advence .htaccess redirect with one exception

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).

How to add file exception in mod_rewrite

I'm trying to except my execute.php from the rewrite down below, the code isn't mine and I don't know any apache, how can I do this?
# BASE REWRITE
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.*)$ index.php?data=$1 [QSA,L]
# (Line to ignore execute.php)
</IfModule>
Let's just answer this, since you are new to this place :-)
For future questions: please read a bit about what and how to ask here, what is expected from you, OK?
Instead of a specific exception rule it is easier to add a condition to your existing rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteCond %{REQUEST_URI} !/execute\.php$
RewriteRule ^/?(.*)/?$ index.php?data=$1 [QSA,END]
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).

Redirect 301 htaccess for specific domain

I try several things for my redirections but without working solution.
I have 2 domains pointing in the same folder/website (mulitshops prestashop) and I would like to redirect some page from the first domain on a specific page and keep domain.
My work :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
But I cant do this with simple Redirect 301 because I can't specify the domain from request URI.
Beause the target page it's not the same according to the domain.
Sorry for my english and thank you in advance for your help.
In addition to what #DusanBajic explained in his comment to your question you also need to consider the difference between absolut and relative path in rewriting rules. This is actually explicitly documented...
WHen implemented in distributed configuration files (".htaccess") the rule pattern is matched against the relative path of the requested URL. You however try to match it against an absolute path which will never match. So either change your patterns to use relative paths too, or, preferably, implement your rewriting rules such that they work in both cases. So also when the rules are implemented in the real http server's host configuration where the pattern is matched against the absolute path inside the requested URL. This appears confusing at first. But it does make total sense, once you think about it.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]
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..
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).

htaccess: add php extension, except when there is a trailing slash

I understand that to hide a website extension, I can use the following regular expression in htaccess.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This will rewrite, e.g., example.com\post to example.com\post.php.
However, how do I modify the script such that it DOES NOT add .php when there is a trailing slash at the end? This is so that it will recognise the index page.
Currently, if I have example.com\folder\, it will rewrite it as example.com\folder\.php.
Thanks.
Instead of explicitly checking for a trailing slash the usual approach is to check if the request actually references a directory in the server side file system. That can be implemented like this, which also checks if such a php file actually does exist:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)/?$ /$1.php [END]
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).
Use another RewriteCond to check the URI for a trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can find a working example of this here:
https://htaccess.madewithlove.be?share=d1fa1042-e671-5b7d-ba96-572510689e8b

Redirecting to another domain with query strings

I am moving to another domain and I would like to redirect dynamic and static ULRs too. I have tried many .htaccess redirect rules but they don't work. For example:
FROM: http://www.bercsekft.hu/motorosbolt/index.php?id=546
TO: https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^id=509$
RewriteRule ^(.*)$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301,L]
I have also another rules in .htaccess. I am not sure if they have affect to the redirect rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^bercsekft.hu$
RewriteRule ^(.*)$ "http\:\/\/www\.bercsekft\.hu\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^jarmu\/?$ "http\:\/\/www\.bercsekft\.hu\/motorosbolt" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edeny\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^bercsekft\.hu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bercsekft\.hu$
RewriteRule ^edenybolt\/?$ "http\:\/\/konyhafelszereles\.bercsekft\.hu\/" [R=301,L]
RewriteBase /
RewriteRule ^sitemap.xml$ motorosbolt/sitemap2.php [L]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php52” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php52___lsphp .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Loos like your issue is the location of that dynamic configuration file you use (".htaccess") in combination with the paths you defined in there. If you really need to use a dynamic configuration file then it usually is easiest to place it in the http servers DOCUMENT_ROOT folder. You need to make sure that such files are considered at all by the http server (hint: the AllowOverride directive) and that the file is readable by the http server process.
This is a slightly modified version of your code to adjust the paths, also I replaced the numerical ID, your question contradicts itself in that detail:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)id=546(&|$)
RewriteRule ^/?motorosbolt/index\.php/?$ https://www.bercsemotor.hu/k/robogo-alkatreszek/aprilia-alkatreszek/aprilia-kormany-alkatresz [R=301]
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...
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).

Resources