What is wrong in my htaccess regular expression? - regex

I want to convert this beauty of URL
http://insenti.ru/425/sayt-dlya-odinokih-ne-vybirayut/&page=2
into this
http://insenti.ru/texts.php?nead_id=425&page=2
So I use this regular expression in my htaccess:
RewriteRule insenti\.ru\/([0-9]+)\/(.*)\/(.*) insenti.ru/texts.php?nead_id=$1$3
But it has a mistake and the redirect does not work.
Here is my entire htaccess file:
ErrorDocument 404 http://insenti.ru/search/err=nopage
RewriteEngine On
RewriteRule (album|chat|help|index|landing|process_data|search|signup|sympathy|user|userspecs)/(.*) $1.php?$2
RewriteRule insenti\.ru\/([0-9]+)\/(.*)\/(.*) insenti.ru/texts.php?nead_id=$1$3
RewriteCond %{HTTP_HOST} !^insenti.ru$ [NC]
RewriteRule (.*) http://insenti\.ru/$1 [R,L]
When I check it using php preg_replace it works perfect:
$link = preg_replace('/insenti\.ru\/([0-9]+)\/(.*)\/(.*)/i', 'insenti.ru/texts.php?nead_id=$1$3', $link);
I will appreciate so much any help with that!!

RewriteRule doesn't match domain name. You can use:
RewriteEngine On
RewriteRule ^(\d+)/[^/]+/(.*)$ texts.php?nead_id=$1$2 [L,NC]

Related

Regex url redirect in apache

i want to redirect pages in apache, so i tried this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?page_id=671 http://letempspourtoit.fr/news [L,R=301]
</IfModule>
but it just redirect to the home page. So i tried to escape the question mark like this :
RewriteRule ^/\?page_id=671 http://letempspourtoit.fr/news [L,R=301]
no success...
Any hint greatly appreciated!
RewriteRule works on URL-paths, and query strings are not considered part of the path. You need a RewriteCond on QUERY_STRING:
RewriteCond %{QUERY_STRING} ^page_id=671$
RewriteRule ^/$ http://letempspourtoit.fr/news [L,R=301]
As you can see, the path for the RewriteRule is /.
Your RewriteRule worked for URLs like http://host/%3Fpage_id=671, that is: URLs with a %-encoded question mark.

RewriteRule to add missing parameter to URL Path

I'd like to use a RewriteRule to change an URL of the format
http://server.com/srv/index.php/12345/somedynamichash/somedynamichash/somedynamichash
where /srv/index.php/12345/ is static to an URL of the format:
http://server.com/srv/index.php/12345/XXXX/somedynamichash/somedynamichash/somedynamichash
if and only if XXXX is missing in the request.
Neither this
RewriteRule "^/(srv/index\.php/\d+/)(?!XXXX(?:$|/))(.*)" "$1XXXX/$2" [R]
nor this combination
RewriteRule ^/srv/index\.php/(\d+)/(XXXX/.*)$ srv/index.php/$1/$2 [L]
RewriteRule ^/srv/index\.php/(\d+)/(.*)$ srv/index.php/$1/XXXX/$2 [L]
worked.
RewriteCond is the solution. It will apply the RewriteRule below only if the $1 does not match the XXXX:
RewriteEngine on
RewriteCond "$1" "!^XXXX$"
RewriteRule "^/srv/index.php/12345/([^/]+)/(.*)$" "http://localhost/srv/index.php/12345/XXXX/$1/$2"
This rule should work for you in site root .htaccess or in Apache config:
RewriteEngine On
RewriteRule ^/?(srv/index\.php/\d+)/(?!XXXX(?:/.*)?$)(.*)$ /$1/XXXX/$2 [L,NC,R=302,NE]

%{REQUEST_URI} doesn't seem to work with index.php

I have a website that uses the following format of links:
http://www.website.com/section1/index.php
http://www.website.com/section2/index.php
http://www.website.com/section3/index.php
http://www.website.com/section1/section4/index.php
What I was trying to do is to get rid of the last part "index.php" by using the following .htaccess directives:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L]
Of course, www.google.com is just for testing purposes, however the example below doesn't work. What is wrong with it? The second part of the question is what do I replace www.google.com with if I want to rewrite to http://www.website.com/section1 ?
Thank you!
It is not working because your regex is incorrect. RewriteCond %{REQUEST_URI} ^/index\.php$ is expecting %{REQUEST_URI} to be /index.php but you have /section1/index.php.
Correct version will be:
RewriteEngine on
RewriteCond %{REQUEST_URI} /index\.php$
RewriteRule (.*) http://www.google.com [R=301,NC,L]
Or even:
RewriteEngine on
RewriteRule /index\.php$ http://www.google.com [R=301,NC,L]

htaccess RewriteRule special characters

I have this line of code:
RewriteRule ^account/?edit=([A-Za-z]+)$ /?goTo=account&act=edit_$1 [L,NC]
When I go to mysite.com/account/?edit=username it is supposed to refer to mysite.com?goTo=account&act=edit_username but it gives me error 404
Any help?
Thanks!
You cannot match QUERY_STRING using RewriteRule. That requires a RewriteCond like this:
This should work:
RewriteCond %{QUERY_STRING} (?:^|&)edit=([^&]*) [NC]
RewriteRule ^account/?$ /?goTo=account&act=edit_%1#something [L,NC,NE,QSA]
Reference: Apache mod_rewrite Introduction

Redirecting with mod_rewrite not working correctly

I am new to using the mod_rewrite engine on Apache although I do have some basic understanding of regular expressions. However, I am perplexed and becoming quite apoplectic at a redirect that isn't working correctly.
Here is the relevant code in my .htaccess file:
RewriteCond %{REQUEST_URI} ^/vacations/([^/]*)(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
When I redirect, there is nothing in $2. $0 and $1 both contain the entire matched string. For example, if the original URL is /vacations/asia/rar, I will be redirected to http://www.example.com/vacations/asia/rar instead of www.example.com/asia.
Any help will be greatly appreciated.
This is because you are using RewriteRule backreferences ($1) when you should be using the RewriteCond backreferences (%1):
RewriteCond %{REQUEST_URI} ^/vacations/([^/]*)(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/%1 [R=301,L]