Rewrite rule with 2 variables with "/" separation - regex

I have an .htaccess file located in a folder "/mixtapes/" I am trying to get the URL mydomain.com/music/downloads/mixtapes/this-title/id to execute mydomain.com/music/downloads/mixtapes/item.php?title=variable1&id=variable2
I currently have the below way somewhat working but it only uses the id and I need both variables (../mixtapes/title/id)separated by "/" and for some reason with the below code the index page inside "/mixtapes/" does not work.I am stumped! I am somewhat new to this and any help is greatly appreciated!
BTW on my index page the passing url to item.php page is rewritten to <a href="title/id">I just cant seem to get it to properly execute item.php?title=a&id=b with the format mixtapes/title/id
Current htaccess file located in "/mixtapes/"
# turn mod_rewrite engine on
RewriteEngine On
# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule ^mixtapes/item.php(.*) - [L]
RewriteRule ^(.*) item.php?id=$1 [QSA]

The comments in your .htaccess actually state wrong
# turn mod_rewrite engine on
RewriteEngine On
# if requested URL is NOT a existing file
RewriteCond %{REQUEST_FILENAME} !-f [OR]
# or if requested URL is NOT a directory
RewriteCond %{REQUEST_FILENAME} !-d
# CSS, images, JavaScript are files and we will never pass this point when those are requested, next rules can be skipped
# rewrite rules
# rewrite /mixtapes/title/id to item.php?title=title&id=id
Rewrite ^mixtapes/([^/]+)/([0-9]+) item.php?title=$1&id=$2 [L]
# catch all other requests and handle them ( optional, default would be 404 if not a physically existing file )
Rewrite (.*) index.php [L,QSA]
I've assumed that your id is a numeric value.
Be aware with the use of the title in php. Don't output this directly but you can use it to verify your URL and redirect wrong title/id combos

Related

Url rewriting using RewriteCond whrere different url required based on query param

My requirement is
domain.com/src/home.php?page=category&category=recipe
domain.com/src/home.php?page=article&key=myarticle
domain.com/src/home.php
Expected end url
domain.com/src/category/recipe
domain.com/src/article/myarticle
domain.com
I have written one
RewriteRule ^src/([A-Za-z0-9-]+)/?$ src/home.php?page=article&key=$1 [NC,L]
The problem is if one is working then the other is not.
Can anyone please suggest how to make it work with some condition based on "page" query param.
Thanks in advance.
With your shown samples, attempts, please try following htaccess Rules file. Make sure to keep your htaccess file along with src folder(not inside it).
Also please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /src/
##Rules for handling url domain.com/src/category/recipe here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]
##Rules for handling urls like: domain.com/src/article/myarticle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]
##Rule for handling domain.com url here.
RewriteRule ^/?$ src/home.php [QSA,L]

Apache Rewrite rule to manage language redirections

I am trying to structure a rewrite rule to implement redirection based on languages.
My directory structure is as follows:
.
├── .htaccess
├── assets
│   ├── css
│ │   └── master.min.css
│   ├── fonts
│   │   ├── fontawesome-webfont.eot
│   │   ├── fontawesome-webfont.svg
│   │   ├── fontawesome-webfont.ttf
│   │   ├── fontawesome-webfont.woff
│   │   ├── fontawesome-webfont.woff2
│ │   └── FontAwesome.otf
│   ├── img
│   │   ├── logo.svg
│   │   ├── slide1.jpg
│   │   ├── slide2.jpg
│   │   ├── slide3.jpg
│ │   └── slide4.jpg
│   └── js
│    └── scripts.min.css
├── de
│   ├── index.php
│   ├── sie.php
│   ├── uns.php
│   └── zusammen.php
├── en
│   ├── index.php
│   ├── together.php
│   ├── us.php
│   └── you.php
├── fr
│   ├── ensemble.php
│   ├── index.php
│   ├── nous.php
│   └── vous.php
└── it
    ├── index.php
    ├── insieme.php
    ├── noi.php
    └── voi.php
I have no root index.html or index.php file. I want to have the .htaccess redirect the user to one of the index files inside the language directories by sniffing the browser language and then redirecting the user to the appropriate language. The default language, when the browser's language cannot be sniffed should be French.
My current .htaccess file consists of the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de|fr|it)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^(.*)$ /fr/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /it/$1 [L]
This seem to function partially, but it isn't able to access the various assets such as images, javascripts, or fonts.
Once the page loads, thereafter, there shouldn't be any problems navigating and changing languages as I have used internal URLs that are directly linking to the specific pages in the appropriate directories.
Any help would be appreciated. Thank you.
You can detect and route using browser's languages. Place this code in root .htaccess
RewriteEngine On
RewriteRule ^(assets|en|de|fr|it)/ - [L,NC]
# detect browser language and capture first 2 chars
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
# current request is not pointing to a real file
RewriteCond %{REQUEST_FILENAME} !-f
# check if corresponding directory exists
RewriteCond %{DOCUMENT_ROOT}/%1/ -d
RewriteRule ^ %1%{REQUEST_URI} [L]
## default fr rule
# current request is not pointing to a real file
RewriteCond %{REQUEST_FILENAME} !-f
# current request is not pointing to a real directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/?$ fr%{REQUEST_URI} [L]
I don't know how you try include assets in you source code, So you can try make this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(img|css|js|fonts)/(.*)$ assets/$2/$3 [QSA,L]
RewriteRule ^(.*)/(fr|it|en|de)/(.*)$ $2/index.php?area=$3 [QSA,L]
RewriteRule ^.htaccess$ - [F]

redirect to maintenance page issue

My maintenance page to my site has a rather odd glitch, the page is redirecting but something in the htaccess code is blocking the css and javascript files. Please take a look at my code there's something in it that's not quite right, because5 when I remove the redirect the css loads as normal.
#
# Redirection to the Maintenance Page
RewriteCond %{REMOTE_ADDR} !^25\.210\.157\.0
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif)
RewriteRule .* /maintenance.html [R=307,L]
#
#
Found the answer, but is there any easier way, instead of selecting all type of file to an all encompassing command?
RewriteCond %{REQUEST_URI} !\.(jpg|jpeg|png|gif|css|js|ico|woff|eot|svg|ttf)
You can use:
RewriteCond %{REMOTE_ADDR} !^25\.210\.157\.0
RewriteCond %{REQUEST_URI} !/maintenance\.html$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /maintenance.html [R=307,L]

Error getting .htaccess to direct googlebot using _escaped_fragment_

I am trying to get my pages indexed on google using a prerendering service for my backbone app.
I know the setup works fine when I specifically add googlebot to the useragent list but Ive been advised against this in favor of using the _escaped_fragment_ method. Only problem is the _escaped_fragment_ parameter isn't getting passed correctly. Can some help please?
thanks!!!
# html5 pushstate (history) support:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "xxxxxxxx"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Proxy the request
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/https://www.example.com/$2 [P,L]
# If non existent
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L,QSA]
</ifModule>
All the apache modules are loaded and working.
So the .htaccess is actually correct... here Google's official answer.
Quote from http://productforums.google.com/forum/#!category-topic/webmasters/crawling-indexing--ranking/bZgWCJTnl08%5B1-25%5D by John Mueller (google employee)
Looking at your blog's homepage, one thing to keep in mind is that the Fetch
as Googlebot feature does not parse the content that it fetches. So when you
submit toddmoyer.net/blog/ , it fetches that URL. After fetching the URL, it
doesn't parse it to check for the "fragment" meta tag, it just returns it to
you. However, if you fetch toddmoyer.net/blog/#! , then it should rewrite the
URL and fetch the URL toddmoyer.net/blog/?_escaped_fragment_= .
When we crawl and index your pages, we'll notice the meta-tag and act
accordingly. It's just the Fetch as Googlebot feature that doesn't check for
meta-tags, and instead just returns the raw content.

Combine "folder as GET parameter" and "subdomain as GET parameter"

I have a site that rewrites path after domain to a page GET parameter.
##REWRITING DIRECTORIES TO GET PARAMETERS
RewriteBase /
#Ignore all real directories (do not rewrite them)
RewriteCond %{REQUEST_FILENAME} !-d
#Also do not rewrite real files
RewriteCond %{REQUEST_FILENAME} !-f
#For everything else, index.php should fetch the proper content
RewriteRule ^([^/]*)$ index.php?page=$1 [QSA,L]
##This means:
#example.com/help
#~becomes~
#example.com?page=help
The site uses multiple languages and so far, I've been using cookies to set and remember the language for the user. While the convenience for the user is disputable, this is definitely not convenient for the SEO.
I need to rewrite [a-z]{2}\.mydomain\.xx to index.php?lang=$1 so that user will be always on en.domain.com for example. There are examples to do this, however I'm still confused about how the rewrite engine works and I don't know how should I combine my new rules with the old ones:
##Language rewrite
#Copypasted. Didn't understand
RewriteCond %{HTTP_HOST} ^([a-z]{1,2})\.domain\.xx
RewriteRule ([a-z]{1,2})\.domain\.xx index.php?lang=$1 [QSA,L]
How can I get en.domain.com/help turn in index.php?page=help&lang=en?
How can I get en.domain.com/help turn in index.php?page=help&lang=en
You can use:
RewriteEngine On
RewriteBase /
#Ignore all real directories (do not rewrite them)
RewriteCond %{REQUEST_FILENAME} !-d
#Also do not rewrite real files
RewriteCond %{REQUEST_FILENAME} !-f
#For everything else, index.php should fetch the proper content
RewriteCond %{HTTP_HOST} ^([a-z]{1,2})\.domain\.xx$ [NC]
RewriteRule ^([^/]+)/?$ index.php?lang=%1&page=$2 [QSA,L]
Reference: Apache mod_rewrite Introduction