htaccess rewrite rule not working for http:// - apache

My rewrite rule is
RewriteRule ^site/([^/]+)$ page.php?site=$1 [L]
It is working fine for
http://example.com/site/google.com
But not working when i add http:// or https:// before google.com like
http://example.com/site/http://google.com
OR
http://example.com/site/https://google.com
Please share a rewrite rule which can accept everything after /site/ like http://example.com/site/here_it_may_be_any.

You need to capture http:// type string from RewriteCond otherwise Apache strips multiple slash to single in RewriteRule pattern. Use this rule:
RewriteCond %{REQUEST_URI} /site/(.+)$ [NC]
RewriteRule ^ page.php?site=%1 [L,QSA]

Related

Apache .htaccess: Redirect to an URL containing a hashtag

The URLs
https://example.com/moved/
https://www.example.com/moved/
should be redirected to the URL
https://my.example.com/#views/settings.php?id=2
via .htaccess.
This is what I have tried:
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond %{REQUEST_URI} moved$
RewriteRule (.*)$ https://my.example.com/#views/settings.php?id=2 [R=301,L]
However it seems not to work. I guess the reason is the #hashtag within the URL where I want to redirect to (it marks a comment within .htaccess).
How to redirect this correctly?
Change your rule to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^moved/?$ https://my.example.com/#views/settings.php?id=2 [R=301,L,NE,NC]
^(?:www\.)?example\.com$ will match www.example.com and example.com
Flag NE is used for no escaping of # in redirected URL
Pattern moved/?$ matches /moved/ and /moved, thus making trailing slash optional.
Make sure to clear your browser cache or use a new browser for testing.

Apache Redirect // to no //

An email went out with the wrong link (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy//) and we need to redirect the // to (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy/) but no matter what I try, the redirect isn't working.
They also want it to be redirected to always have https:///www at the beginning and to never have index.html at the end, so already in the .htaccess file I have:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^content\-marketing\-strategy/index\.html$ /digital-marketing/content-marketing-strategy/? [L,R=301]
I've tried adding a new RewriteRule, but this won't work:
RewriteRule ^content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]
I'm very new to Apache and redirects so any help is much appreciated! Thank you!
Edit: Of note, this is in an .htaccess file inside of the digital-marketing folder (https://www.digitalmarketer.com/digital-marketing/.htaccess) which was done so all the above rules would only apply to the digital-marketing folder.
You can use insert rule at the end of your other rules to strip multiple // into /:
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /digital-marketing/$0 [R=301,L,NE]
Apache automatically strips down multiple // into one inside the pattern for RewriteRule thus captured value $0 will have all // converted into /
You can write a wildcard expression to remove trailing slashes. The below will match any HTTP or HTTPS URL that trails in a forward slash, and remove all trailing forward slashes from that URL:
RewriteRule ^(.*)/+$ $1 [R=301,L]
And more using 301 redirects, see more here: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)
Good luck!
I see nothing in the way that the rule is written that would make it not rewrite. However you have multiple rules with the L flag that might stop processing on the rewrite at an earlier point than you are looking for. From the documentation
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
(https://httpd.apache.org/docs/current/rewrite/flags.html).
You can try this page out http://htaccess.mwl.be/ to test all your rules together. You might have to rewrite them a bit to work with that page, it's not aware of the level your .htaccess file is at so you will have to rewrite all your rules to trigger from the root for example: RewriteRule ^digital\-marketing/content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]

.htaccess Rewrite Rules, friendly URLs + redirect

I'm using .htaccess to take URLs that look like:
/index.php?page=2
and rewrite them to, for example:
/contact-us
I want to do two things:
When loading /contact-us, show page /index.php?page=2 but keep the friendly URL. I know this looks something like:
RewriteRule ^contact-us$ "index\.php\?page\=2" [L]
Which does work ok. But now I also want people who navigate to /index.php?page=2 to end up on /contact-us - how do I achieve this as a 301 redirect in combination with the friendly URL rewrite?
Not sure why you are using quotes and escapes in target portion of your rewrite rule. The target portion is not a regular expression. That can simply look like:
RewriteRule ^contact-us$ /index.php?page=2 [L]
To redirect the index.php?page=2 you will need to do the following. This rule MUST be before the rule above or you will get in rewrite loop.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=2$
RewriteRule .* /contact-us [R=301,L]
Here you are setting two rewrite conditions: that the page is /index.php and that the query string is page=2 (and only page=2). The R=301 flag will force an external rewrite to /contact-us and also send and HTTP 301 header.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?page=2[&\s] [NC]
RewriteRule ^ contact-us? [R=302,L,NE]
RewriteRule ^contact-us/?$ index.php?page=2 [L,QSA,NC]

Redirect m.example.com to m.example.com/subdir

how do i redirect m.example.com to m.example.com/subdir
i am using the following code for the same but it is getting cyclic and not redirecting properly
RewriteEngine on
RewriteCond %{HTTP_HOST} ^m.example.com$ [NC]
RewriteRule (.*)$ http://m.example.com/subdir [R=301,L]
I cannot also use:
redirect 301 / http://m.example.com/subdir
as my main www site will also get redirected
You need to add a condition so it doesn't redirect if the request is already for /subdir/. Add this somewhere above your rewrite rule:
RewriteCond %{REQUEST_URI} !^/subdir
You're also missing a backreference in your rule:
RewriteRule (.*)$ http://m.example.com/subdir/$1 [R=301,L]
If the only thing you want to redirect is requests for the root (URI=/), then you don't need the extra rewrite condition, only change your regular expression in your rule:
RewriteRule ^$ http://m.example.com/subdir [R=301,L]
To redirect just / but not anything under it you can use something like this in your m.example.com <VirtualHost>
RedirectMatch ^/$ http://m.example.com/subdir/
Alternatively, if you want to redirect everything into subdir except requests for subdir itself you could use mod_rewrite
RewriteCond %{REQUEST_URI} !^/subdir
RewriteRule ^(.*)$ http://m.example.com/subdir$1 [R=301,L]

ModRewrite weird redirect behavior on removing WWW

I'm trying to use some rule on my project to remove www from the beginning of the URL but I've some problem.
my server structure is:
domain.com/beta_folder
domain.com/beta_folder/page+type
domain.com/beta_folder/page+type/content+name
domain.com/beta_folder/page+type/content+name/edit
domain.com/beta_folder/page+type/content+name/etc.
domain.com/beta_folder/.htaccess //here is where my htaccess is
beta_folder is the site folder, and content+name are content vars, created to retrieve pages from the database.
the site works perfect with this rules
RewriteEngine On
RewriteRule ^(page\+type/)([a-zA-Z0-9_+-]+)[/]?$ page_folder/page.php?varname=$2
My intention was to remove www, so I've added this rule but it isn't effective
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com$1 [R=301,L]
RewriteRule ^(page\+type/)([a-zA-Z0-9_+-]+)[/]?$ page_folder/page.php?varname=$2
My problem starts if I digit www in front of my domain name:
this works
http://domain.com/beta_folder/page+type/content+name
if i write
http://www.domain.com/beta_folder/page+type/content+name
the rewrite rule redirect me at
http://www.domain.compage+type/content+name
if i remove the www rules, the problem still active
unfortunately, I can't make a public test for my domain
basically, if I write
http://www.domain.com/beta_folder
the rules sends me to
http://domain.com/
the only way I've found to solve the problem is to write the folder where my site is, so:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/beta_folder/$1 [R=301,L]
I don't undestand why I should write the folder, because my rewrite cond affects
http://www.domain.com/folder_name/contents and not http://www.domain.com/contents
why folder_name missing from the rewriting?
Try this, subtly different example of your rule:
RewriteCond %{HTTP_HOST} !^www.domain\.com$
RewriteRule (.*) http://domain.com/$1 [R=301,L]
Shout out if this doesn't solve your problem.