Can Domain and Subdomain have same URL pattern on same server(.htaccess)? - regex

I am creating mobile site for an existing site using "m" subdomain.
I am stuck with htaccess rules. I am very new to htaccess and very bad with regex.
Site Name - xyz.com
Subdomain - m.xyz.com
I want that whenever my webpage is redirected( OR directly opened in) to mobile, subdomain should
have exact same URL as main domain but should go to a mobile version file i.e.
Player
domain - xyz.com/5 -- real url - xyz.com/account.php?ln=5 (this account.php is in root folder)
Subdomain - m.xyz.com/5 -- real url - m.xyz.com/account.php?ln=5 (this account.php is in subfolder m)
Team
domain - xyz.com/TeamName/10 -- real url - xyz.com/team.php?ln=10 (this team.php is in root folder)
Subdomain - m.xyz.com/TeamName/10 -- real url - m.xyz.com/team.php?ln=10 (this team.php is in subfolder m)
Game
domain - xyz.com/GameName/18 -- real url - xyz.com/game.php?p=18&t=game (this game.php is in root folder)
Subdomain - m.xyz.com/GameName/18 -- real url - m.xyz.com/game.php?p=18&t=game (this game.php is in subfolder m)
xyz.com works perfectly for all combinations but when I hit subdomain it does not point to correct file.
when i try to hit m.xyz.com/TeamName/10 it redirects to http://m.xyz.com/m/game.php/?p=45&t=TeamName but should have gone to
m.xyz.com/team.php?ln=5 (to the file in subfolder m). Direct URL works.
Directory Structure :
xyz(htdocs)
|
--- account.php
|
--- team.php
|
--- game.php
|
--- m (Subdirectory - Subdomain)
|
--- account.php
|
--- team.php
|
--- game.php
.htaccess file -
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !([a-zA-Z0-9]\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
#Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
#player 1 Parameter = Account Id
RewriteRule ^([0-9]+)\/?$ account.php?ln=$1 [NC]
#team 2 parameter = team name + team ID
RewriteRule ^([a-z0-9_-]+)\/([0-9]+)\/?$ team.php?ln=$2 [NC]
#game page = game ID + game Name + type
RewriteRule ^([a-z0-9_-]+)\/([a-z0-9_-]+)\/([0-9]+)\/?$ game.php?p=$3&t=$2 [NC]
# End of Apache Rewrite Rules
</IfModule>
Please have a look and let me know if you have any answer, searched on google for almost three days already no luck yet.

Ahh Nailed it.. it was a very simple catch .. dint realize i was missing the .htaccess in my subdomain folder (root/m). Copied the same htaccess in subdomain and it worked like charm. in simple words for other people who may try to find the answer for it...
Domain and subdomain both require a .htaccess file, if you are trying to create an exact domain copy site for mobile site you must be having files with same name in your subdomain directory you need to just place a copy of htaccess file in your subdomain which you are using for domain.
Dint realize it was so easy... stupid me... (This is how you learn :D )

Related

Regex redirect of wordpress blog from subdomain to domain

I am trying to redirect the WordPress blog post from old to new domain.
We had a blog on subdomain http://blog.domain.xyz/ and after the migration is on main domain https://www.domain.xyz/
On the old blog, URL of the blog post was:
http://blog.domain.xyz/2020/03/25/post-name
(part /2020/03/25/ is just an example of date)
now I need it to redirect to:
https://www.domain.xyz/post-name
I matched with regex domain and date part:
http\:\/\/blog.domain.xyz\/\d{4}\/\d{2}\/\d{2}\/
I know how to redirect manually all posts one by one, but there are more than 1000 posts, so this is not an option.
I can't figure out how to take post-name part and apply it to the new domain
I think you need something like the following.
RewriteRule ^/\d+/\d+/\d+/(.*)$ https://www.domain.xyz/$1 [R=301,L]
// ^/ start at the root
// \d+/\d+/\d+/ match date folders like 2020/03/25/
// (.*) the part we want to keep "some-slug"
// $ end of match
// $1 put the part we want to keep here "some-slug"
Basically matching any urls with /2020/03/25/post-name and redirects to https://www.domain.xyz/post-name.
Note: this assumes you're adding the redirect at the old domain.
In the meantime I figured it out:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.domain.xyz$
RewriteRule \d{4}/\d{2}/\d{2}(.*)$ https://www.domain.xyz$1 [R=301,L]
</IfModule>

Apache2 rewrite(exclude) part of path

I have a multy language site serverd by Apache2 webserver. Structure is
site.com - main version
site.com/fr - for france
site.com/it - another lang
I have a problem when i am trying to get static resources which are located in root direcory, like
site.com/js/bundle.js - right
site.com/fr/js/bundle.js - wrong, because CMS sets base URL as site.com/fr
How can i strip language part from URL with .htaccess for static files img, js etc.
All languages are known.
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_URI} ([\w]+)/([\w]+).\(js|css|vbs)$
RewriteRule ^ %1/%2.%3 [R,L]

301 redirect strip QUERY_STRING

I am planning to relaunch a website soon. Part of the relaunch is a switchover from Joomla! to Wordpress.
Besides some old pages, which will be redirected with some exact expression, I have a lot of pages, which will be redirected to the new root page.
Whats the right regular expression, if I want to redirect URLs like these:
http://www.somedomain.com/index.php?somevar=123&com_option=somevalue
http://www.somedomain.com/index.php?com_option=someothervalue
All the URLs have in common:
http://www.somedomain.com/index.php?
+
com_option
How can I redirect all those pages with one line of redirect code in .htaccess?
I assume you do not need somevar and only want to carry over com_option
RewriteEngine on
RewriteBase /
RewriteRule index.php(.*)com_option=(.*)$ http://newsite.com/index.php?com_option=$2 [L,R=301]
You will need to do something like this:
RedirectMatch 301 (.*)/the/old/domain/and/url/(.*) http://the/new/domain/and/path/$2
Now, that the site is gone live, I see that none of the answers to my questions worked out.
After some trial an error I found a solution:
By now Google offers a live-demo:
1) Go to google & search for "site:sqlxpert.de".
2) Click on one off the links having "option" in the URL.
3) You will be redirected to the new pages root - which in this case is the same domain and also an index.php, but the d*** QUERY_STRING is gone.
I needed a .htaccess snippet, which does the following (expressed in words):
If requested file is
INDEX.PHP or INDEX2.PHP
AND
QUERY STRING contains "option"
then 301 REDIRECT to
http://www.sqlxpert.de/
WITHOUT any QUERY STRING
The solution:
RewriteCond %{QUERY_STRING} option
RewriteRule ^index\.php http://www.sqlxpert.de/$1? [L,R=301]
RewriteRule ^index2\.php http://www.sqlxpert.de/$1? [L,R=301]

Mod Rewrite Regex - Match a url containting A but not B

I'm trying to send page requested through a front controller.
I need to match the following urls
domain.com/admin/
domain.com/admin/somepage
but I also have some assets in the /admin subfolder that I DONT want to match
domain.com/assets
domain.com/assets/mything.css
domain.com/assets/mything.css
domain.com/xml/myxml.xml
I have written the following rule (inside .htaccess in the root of the site and that works for all 5 example URLs. How do I get it to match the top two ONLY?)
RewriteBase /
# index.php is the front controller
RewriteRule ^(admin) admin/index.php [L,QSA]
There are two ways I can see of doing this (I'd like to know how to do it using the folder 'assets' and 'xml' as an exclusion AND by setting extensions)
I'd use a negative RewriteCond before the final routing rule along these lines:
RewriteCond %{REQUEST_URI} !assets

mod_rewrite per-dir redirection returning a 400

I moved my images directory to a different folder, and now I want to redirect all images requests from that folder to the new one.
I do not have access to the main configuraion file, so I'm doing this in a .htaccess.
I tried this, and it works:
RewriteCond %{REQUEST_URI} old_dir/.+\.(jpg|png|gif)$
RewriteRule old_dir/(.+[^/]+\..+)$ $1 [L,PT]
But since they have permanently moved, I want to do a proper redirect, so I added the [R] flag, like this:
RewriteCond %{REQUEST_URI} old_dir/.+\.(jpg|png|gif)$
RewriteRule old_dir/(.+[^/]+\..+)$ $1 [L,PT,R]
But the server gets confused, and returns a 400, so I looked at the log file, and this is what happens:
strip per-dir prefix: C:/wamp/www/natrazyle/old_dir/images/banner.jpg -> old_dir/images/banner.jpg
applying pattern 'old_dir/(.+[^/]+\..+)$' to uri 'old_dir/images/banner.jpg'
rewrite 'old_dir/images/banner.jpg' -> 'images/banner.jpg'
add per-dir prefix: images/banner.jpg -> C:/wamp/www/natrazyle/images/banner.jpg
explicitly forcing redirect with http://localhost/C:/wamp/www/natrazyle/images/banner.jpg
As you can see, the full local path gets added after localhost
I know I'm doing something wrong, I just can't figure it out myself.
Any help would be greatly appreciated...
Use an absolute URL path in your substitution:
RewriteRule ^old_dir/(.+\.(jpg|png|gif))$ /$1 [L,R]